我有这张桌子:
+--------------+-----------------------------+----------------+
| Username | Message | Status |
+--------------+-----------------------------+----------------+
| jamesbond | I need some help | SendingOK |
| jamesbond | I need some help | SendingOK |
| jamesbond | I need some help | SendingFailed |
| jamesbond | Mission accomplished | SendingOK |
+--------------+-----------------------------+----------------+
由这种 SQL 语法产生:
SELECT A.Username, A.Message, B.Status
FROM db1.SmsBroadcast as A
INNER JOIN db2.sentitems as B
ON A.MessageSMS1 = B.TextDecoded
WHERE A.Username = 'jamesbond'
GROUP BY Message, Status
现在如何有这个输出?
+--------------+-----------------------------+-----------+---------------+
| Username | Message | SendingOK | SendingFailed |
+--------------+-----------------------------+-----------+---------------+
| jamesbond | I need some help | 2 | 1 |
| jamesbond | Mission accomplished | 1 | 0 |
+--------------+-----------------------------+-----------+---------------+
SendingOK
和SendingFailed
列实际上可以使用计算COUNT(*)
,但我不知道如何基于相同的消息进行计数,而这些消息以相同的 SQL 语法执行。知道怎么做吗?