我有一个MySQL查询来生成一个显示两列的视图,这两列是子查询但由于某种原因我收到错误。
SELECT
(SUM(cards_required) FROM `orders` WHERE statuses = 1) As Count,
(SUM(cards_required) FROM `orders` WHERE statuses = 2) As Count
FROM `orders` WHERE user_id = 1
错误消息
#1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法 'FROM orders WHERE statuses = 1附近)作为Count,(SUM(cards_required) 来自第3行的订单
答案 0 :(得分:1)
您使用的语法不太正确。您可以使用IF
有条件地求和:
SELECT
SUM(IF(statuses = 1,cards_required,0)) As status_1,
SUM(IF(statuses = 2,cards_required,0)) As status_2
FROM `orders` WHERE user_id = 1