我有一个存储过程,我必须根据操作员检查比较一些结果。运算符可以是“ >
”或“ <
”
我的查询是:
SELECT COUNT(LastWeekId) , COUNT(PreviousToLastWeekId)
FROM
(
SELECT `transaction_id` AS LastWeekId, NULL AS PreviousToLastWeekId
FROM `kwt_transaction`
WHERE `statusid` =1000
AND (createdon BETWEEN CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 7 DAY)," 00:00:00") AND CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 1 DAY)," 23:59:59"))
UNION
SELECT `transaction_id` AS LastWeekId, NULL AS PreviousToLastWeekId
FROM `kwt_transactions_p2p`
WHERE `statusid` =1000
AND (createdon BETWEEN CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 7 DAY)," 00:00:00") AND CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 1 DAY)," 23:59:59"))
UNION
SELECT NULL AS LastWeekId, `transaction_id` AS PreviousToLastWeekId
FROM `kwt_transaction`
WHERE `statusid` =1000
AND (createdon BETWEEN CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 14 DAY)," 00:00:00") AND CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 8 DAY)," 23:59:59"))
UNION
SELECT NULL AS LastWeekId, `transaction_id` AS PreviousToLastWeekId
FROM `kwt_transactions_p2p`
WHERE `statusid` =1000
AND (createdon BETWEEN CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 14 DAY)," 00:00:00") AND CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 8 DAY)," 23:59:59"))
) ttable
HAVING COUNT(LastWeekId) > COUNT(PreviousToLastWeekId)
所以我想检查一下:
HAVING Case _operaotr
when '>'
COUNT(LastWeekId) > COUNT(PreviousToLastWeekId);
when '<'
COUNT(LastWeekId) < COUNT(PreviousToLastWeekId);
End Case;
在mysql中这可能吗
最佳答案
我认为您可以这样做:
HAVING CASE @Operator
WHEN '>' THEN COUNT(LastWeekId) > COUNT(PreviousToLastWeekId)
WHEN '<' THEN COUNT(LastWeekId) < COUNT(PreviousToLastWeekId)
WHEN '=' THEN COUNT(LastWeekId) = COUNT(PreviousToLastWeekId)
-- and so on
END