我该如何有效地处理行

时间:2018-03-25 06:45:49

标签: php mysql arrays

我有2张桌子

表1:发票

enter image description here

表2:invoice_statuses enter image description here

我有一行如下

enter image description here

这里有两张发票,其中invoice_id = 1,2

当发票状态处于查询状态时,则会计入“pending_reason”。

这里我需要结果集如下:

enter image description here

1 个答案:

答案 0 :(得分:2)

你可以使用不同的invoce_id计数,如果是发布和查询

select count(distinct invoce_id) no_of_invoices, 
       sum(if(s_status = 'posted', 1,0)) invoice_status_posted,
        sum(if(s_status = 'query', 1,0)) invoice_status_query
from my_table

并为您更新..问题....

  select count(distinct invoce_id) no_of_invoices, 
    sum(if(s_status = 'posted', 1,0)) invoice_status_posted,
    sum(if(s_status = 'query' AND pending_reason is null, 1,0)) invoice_status_query, 
    sum(if() s_status = 'query' and pending_reason ='Pending Internal Clariication', 1,0)) Pending_Internal_Clariication

from my_table