表结构是这样的:

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",但这没有帮助。我不想将“解锁”表更改为INT,因为它只有布尔值。如何为每个user_id求和计算解锁表?

最佳答案

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是保留字的列表。

关于mysql - 在MySQL中计算TINYINT值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16022337/

10-11 03:10