mysql - 在一个查询中计算null和non-null字段

时间:2018-06-09 06:03:17

标签: mysql

输入表(名为ruchin

id     comment           order_id          deleted_at
1        abc               1234              NULL
2        abcd              1234              TimeStamp
3        xyz               1234              NULL
4        pqr               1234              TimeStamp
5        as                4567               NULL
6        lo                4567               NULL

我希望输出为

order_id        count(deleted_at==Timestamp)    count(deleted_at==NULL)
1234                   2                                 2
4567                   0                                  2

我希望这是单一查询。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

您可以使用count()函数,因为它不会计算NULL值。

以下是查询:

SELECT order_id, 
       count(deleted_at) as 'del is not null',
       count(order_id) - count(deleted_at) as 'del is NULL' 
FROM ruchin 
GROUP BY order_id