表结构如下:
actions: int(10)
unlock: tinyint(1)
user_id: int(20)
name: varchar(50)
我有这样的疑问:
SELECT SUM(actions) AS "sum_actions", SUM(unlock) AS "sum_unlock", user_id, name
FROM mytable AS `Results` WHERE user_id != 0 GROUP BY user_id
ORDER BY sum_actions DESC LIMIT 0,300
这会导致#1064 - You have an error in your SQL syntax
错误。
当我删除SUM(unlock) AS "sum_unlock"
时,查询才有效。所以我认为不可能总结TINYINT。所以我改为COUNT(unlock) as "count_unlock"
,但这没有帮助。我不想将“unlock”表更改为INT,因为它只有布尔值。如何计算每个user_id的求和解锁表?
答案 0 :(得分:1)
unlock
是保留字。试试这个:
SELECT SUM(actions) AS "sum_actions", SUM(`unlock`) AS "sum_unlock", user_id, name
FROM mytable AS `Results`
WHERE user_id != 0
GROUP BY user_id
ORDER BY sum_actions DESC
LIMIT 0,300
Here是一个保留字列表。
答案 1 :(得分:0)
您可以尝试SUM(CAST(unlock AS INT))
进行计数,就好像该列是INT
列而不实际将其更改为INT
列:
SELECT
SUM(actions) AS "sum_actions",
SUM(CAST(unlock AS INT)) AS "sum_unlock",
user_id,
name
FROM
mytable AS `Results`
WHERE
user_id != 0
GROUP BY
user_id,
name
ORDER BY
sum_actions DESC
LIMIT 0,300