drf65218 2013-12-18 16:18
浏览 37
已采纳

返回PHP中MySQL查询的值

I have a table holding order lines. There are multiple order lines per order number. Against each line there is a number in the 'diff' column which indicates if the line has been supplied complete. Zero or below, the line is complete, 1 or above and the the line is incomplete.

I need to output the total complete orders and the total pending orders.

With some help I've got as for as formulating an SQL query that is close but does not output any results in my PHP script:

$result = mysql_query("SELECT ord_number, MAX( IF( diff <=0,'COMPLETED','PENDING')) FROM  $tabl GROUP BY (ord_number)");

while($row = mysql_fetch_assoc($result)){
    echo $row['COMPLETED'];
    echo $row['PENDING'];
}

If I run the query below direct in PHPMySQL I get the output listed below:

SELECT ord_number, MAX( IF( diff <=0,  'COMPLETED',  'PENDING' ) ) 
FROM tb_raw_orderfill
GROUP BY (
ord_number
)

Output:

00000P1-OR28622 COMPLETED
00000P1-OR28623 COMPLETED
00000P1-OR28624 COMPLETED
00000P1-OR28625 COMPLETED
00000P1-OR28626 PENDING
00000P1-OR28627 COMPLETED
00000P1-OR28628 COMPLETED
00000P1-OR28629 COMPLETED
00000P1-OR28630 COMPLETED
00000S1-OR02107 PENDING
00000S1-OR02108 COMPLETED
00000S1-OR02109 COMPLETED
00000S1-OR02110 COMPLETED
00000S1-OR02111 PENDING

These are correct results,it's almost as if I need to count them somehow not sure. The results should be

Pending: 3

Complete: 11

Any help greatly appreciated.

Edit 1 - Sample Data:

Product Code    Quantity Ordered    QT Allocated    Diff    Number  Date    Quantity Supplied To Date   Status
code1   10  0   0   00000P1-OR28621 14/11/2013  10  A
code2   20  0   10  00000P1-OR28621 14/11/2013  10  S
code3   20  0   0   00000P1-OR28621 14/11/2013  20  S
code1   120 0   0   00000P1-OR28621 14/11/2013  120 A
code2   2   0   0   00000P1-OR28622 14/11/2013  2   A
code2   10  0   0   00000P1-OR28623 14/11/2013  10  A
code1   10  0   0   00000P1-OR28623 14/11/2013  10  A
code3   10  0   0   00000P1-OR28623 14/11/2013  10  A
  • 写回答

3条回答 默认 最新

  • dongwu5801 2013-12-18 16:56
    关注

    This is a corrected version of Jessica's answer:

    SELECT
         a.Status
        ,COUNT( a.ord_number ) AS Total
    FROM (
        SELECT
             ord_number
            ,IF( MAX(diff) <= 0, 'COMPLETED', 'PENDING' ) AS Status
        FROM $table
        GROUP BY ord_number
    ) a
    GROUP BY a.Status;
    

    This should return two rows like so:

    Status    | Total
    ----------|--------
    COMPLETED | ####
    PENDING   | ####
    

    You could then read the results like so:

    $Completed = 0;
    $Pending = 0;
    
    while($row = mysql_fetch_assoc($result)) {
        switch( $row['Status'] ) {
          case "COMPLETED":
              $Completed = $row['Total'];
              break;
          case "PENDING":
              $Pending = $row['Total'];
              break;
        }
    }
    

    If you wanted a single row with with both totals on it then see Itsols's answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器