如何从单个查询中获取数组中的值 [英] how to get value in array from individual query

查看:226
本文介绍了如何从单个查询中获取数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个

Book ID Array

    Array
    (
        [0] => 61
        [1] => 72
        [2] => 78
        [3] => 100
        [4] => 102
    )

现在从另一个给出价格的表table_bookPrice中给出 我希望从table_bookPrice中选择所有价格,其中书ID在给定数组(书ID数组)中,如果table_bookPrice字段中未提及价格,则它将自动为500

now from another table table_bookPrice where price filed is given i want that select all price from table_bookPrice where book id is in given array (book ID Array) and if price is not mentioned in table_bookPrice field then it would be automatic 500

确切的查询是什么

所以我得到的数组是这样的

so the array i got is like this

Book Price Array

    Array
    (
        [0] => 150
        [1] => 100
        [2] => 500 ( not mentioned in table, so it is 500)
        [3] => 300
        [4] => 200
    )

推荐答案

我在工作,因此无法对其进行测试或编译,但我希望我的逻辑是可以理解的.

I am at work so could not test or compile it, but I hope my logic is understandable.

不确定这是否行得通,但可以遵循以下原则

Not sure if this will work but something along these lines

$book_price_array = array(); //contents to be added.

// loop through the array an examine its price by querying your table.
foreach ($book_id_array as $key => $value) {
   $price = mysql_query("SELECT price FROM table_bookPrice 
                                     WHERE book_id = {$value}");
   // there is a price, set the price.
   if ($price > 0 && $price != NULL)  $book_price_array[$key] = $price;

   // there is no price, set the default price
   else  $book_price_array[$key] = 500; 
}

这篇关于如何从单个查询中获取数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆