我有一个小问题,我有3个SQL请求,我想知道是否可以在1个请求中优化它们。
我的3 sql请求:
SELECT FAI, COUNT( FAI ) AS deferred_count
FROM `email_errors`
WHERE STATUS =1
GROUP BY FAI
-------------------------------------------
SELECT FAI, COUNT( FAI ) AS sent_count
FROM `email_errors`
WHERE STATUS =0
GROUP BY FAI
-------------------------------------------
SELECT FAI, COUNT( FAI ) AS Bounced_count
FROM `email_errors`
WHERE STATUS =2
GROUP BY FAI
所以如果可能的话,我试着得到一个输出表,其中有4列结果
FAI|deferred_count|sent_count|bounced_count
我不知道“FAI”是否可以根据请求加入?
最佳答案
SELECT FAI,
sum( status = 0 ) AS send_count,
sum( status = 1 ) AS deferred_count,
sum( status = 2 ) AS Bounced_count
FROM `email_errors`
GROUP BY FAI
关于mysql - 1中优化许多SQL请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19844648/