duanjianshen4871 2010-01-28 15:57 采纳率: 100%
浏览 315
已采纳

如何从单个查询中获取数组中的值

i have a

Book ID Array

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

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

what will be exact query

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
    )
  • 写回答

2条回答 默认 最新

  • dousong1926 2010-01-28 16:07
    关注

    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; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面