MySQL命令中的双ASC

时间:2016-02-15 14:43:47

标签: mysql sql

为什么我不能运行这个MySQL命令?

SELECT *
FROM users
WHERE
    id = 1 OR
    id BETWEEN 2 AND 8
GROUP BY
    username ASC
ORDER BY
    username ASC
LIMIT 5

任何人都可以解释一下吗?

我收到以下错误:

ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right
syntax to use near '= 1 OR id BETWEEN 2 AND 8 GROUP BY username ASC
ORDER BY username ASC LIMIT 5' at line 1

2 个答案:

答案 0 :(得分:2)

您缺少browse.showOpenDialog(MainForm.frame) 条款:

WHERE

您收到的错误是因为SELECT * FROM users WHERE id = 1 OR id BETWEEN 2 AND 8 GROUP BY username ASC ORDER BY username ASC LIMIT 5; 被视为id的别名而MySQL正在期待类似users,[LEFT, RIGHT, INNER, CROSS] JOIN的内容,WHEREGROUP BYORDER BY

LIMIT在MySQL(至少大多数版本)中是多余的,因为ORDER BY中的ASC进行排序(请参阅documentation)。但是,我不会依赖于此,并使用:

GROUP BY

答案 1 :(得分:1)

只需在条件之前添加where,也可以在群组之后删除ASC,这是没有意义的:

SELECT * FROM users where id = 1 
OR id BETWEEN 2 AND 8 
GROUP BY user
ORDER BY user ASC LIMIT 5