PHP:如何使用mysql_multi_query()执行多个查询

时间:2016-06-07 18:02:45

标签: php mysqli mysqli-multi-query

我正在研究两个类似的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的日期,内部和外部数据。我的意思是应该在mysqli_multi_query下面写入什么来检索四个数据。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

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));
}