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条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改