【问题标题】:PHP: How to Execute the Multiple Query using mysql_multi_query()PHP:如何使用 mysql_multi_query() 执行多重查询
【发布时间】:2016-10-07 18:20:41
【问题描述】:

我正在研究两个相似的 SQL,只是不同之处在于第一个查询股票是在两个日期之间计算的,第二个查询使用当前日期。

PHP:

   $sql="select all_dates.value1 as `Date`, sum(coalesce(`Inward(B_Qty)`, '0')) as `Inward`, sum(coalesce(`Outward(B_Qty)`, '0')) as `Outward` 
    from ( select distinct M_Date as value1 from machine where J_Name = '180MLGMV' and DATE(M_Date) between '2016-06-01' and '2016-06-06' 
    union select distinct M_Date from machine where Pre_Req = '180MLGMV' and DATE(M_Date) between '2016-06-01' and '2016-06-06' ) as all_dates 
    left join (select M_Date, sum(A_Prod) AS `Inward(B_Qty)` 
    from machine where J_Name = '180MLGMV' and DATE(M_Date) between '2016-06-01' and '2016-06-06' group by M_Date) 
    as g on g.M_Date = all_dates.value1 
    left join( select M_Date, sum(NoBot) AS `Outward(B_Qty)` 
    from machine where Pre_req ='180MLGMV' and DATE(M_Date) between '2016-06-01' and '2016-06-06' group by M_Date) 
    as f on f.M_Date = all_dates.value1 group by all_dates.value1 ;" ;

    $sql.="select all_dates.value1 as `Date`, sum(coalesce(`Inward(B_Qty)`, '0')) as `Inward`, sum(coalesce(`Outward(B_Qty)`, '0')) as `Outward` 
    from ( select distinct M_Date as value1 from machine where J_Name = '180MLGMV' and M_Date='2016-06-07'
    union select distinct M_Date from machine where Pre_Req = '180MLGMV' and M_Date='2016-06-07' ) as all_dates 
    left join (select M_Date, sum(A_Prod) AS `Inward(B_Qty)` 
    from machine where J_Name = '180MLGMV' and M_Date='2016-06-07' group by M_Date) 
    as g on g.M_Date = all_dates.value1 
    left join( select M_Date, sum(NoBot) AS `Outward(B_Qty)` 
    from machine where Pre_req ='180MLGMV' and M_Date='2016-06-07' group by M_Date) 
    as f on f.M_Date = all_dates.value1 group by all_dates.value1 "; 

    $list1=mysqli_multi_query($sql,$con);
    while($row_list1=mysql_fetch_array($list1))
               { 
                 $b=$b+$row_list1['Inward'];
                $c=$c+$row_list1['Outward'];


}

请建议如何检索每个Query的Date,Inward和Outward的数据。我的意思是在mysqli_multi_query下面应该写什么来检索这四个数据。

提前致谢。

【问题讨论】:

  • 请转至w3schools.com/php/func_mysqli_multi_query.asp。首先要有明确的想法。
  • @RyanVincent 实际上我已经尝试过那里的查询工作绝对正常。我只是对检索值感到困惑。
  • @NanaPartykar 是的。我正在经历那个只是......谢谢。
  • 嘿@RyanVincent,为什么要说对不起人。你给了你的建议。在 SO 中总是受欢迎的。

标签: php mysqli mysqli-multi-query


【解决方案1】:

mysql_multi_query 手册中的此示例说明了如何使用 multi_query 函数获取多个值。

if (mysqli_multi_query($link, $query)) {
    do {
        /* store first result set */
        if ($result = mysqli_store_result($link)) {
            while ($row = mysqli_fetch_row($result)) {
                printf("%s\n", $row[0]);
            }
            mysqli_free_result($result);
        }
        /* print divider */
        if (mysqli_more_results($link)) {
            printf("-----------------\n");
        }
    } while (mysqli_next_result($link));
}

【讨论】:

  • 好的。我会处理的。谢谢。
猜你喜欢
  • 2015-03-23
  • 2021-10-30
  • 2016-03-11
  • 2014-08-09
  • 1970-01-01
相关资源
最近更新 更多