SQL 关键字组附近的语法不正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5578061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Incorrect Syntax near Keyword Group
提问by Steffan Harris
Hello I'm writing an sql query But i am getting a syntax error on the line with the GROUP BY. What can possibly be the problem.
您好,我正在编写一个 sql 查询,但是我在使用 GROUP BY 时遇到语法错误。可能是什么问题。
SELECT au_lname, au_fname, t.title_id
from authors As a INNER JOIN
titleauthor As ta On a.au_id = ta.au_id INNER JOIN
titles As t On t.title_id = ta.title_id
ORDER BY au_lname, au_fname
GROUP BY au_lname
回答by Tommi
Place GROUP BY before ORDER BY. Also, all fields included in the select must either be in the GROUP BY statement, or be inside an aggregate function.
将 GROUP BY 放在 ORDER BY 之前。此外,选择中包含的所有字段都必须在 GROUP BY 语句中,或者在聚合函数中。
Try this:
尝试这个:
SELECT au_lname, au_fname, t.title_id
from authors As a INNER JOIN
titleauthor As ta On a.au_id = ta.au_id INNER JOIN
titles As t On t.title_id = ta.title_id
GROUP BY au_lname, au_fname, t.title_id
ORDER BY au_lname, au_fname
If you just want to get rid of duplicate results, you can also use keyword DISTINCT:
如果你只是想摆脱重复的结果,你也可以使用关键字 DISTINCT:
SELECT DISTINCT au_lname, au_fname, t.title_id
from authors As a INNER JOIN
titleauthor As ta On a.au_id = ta.au_id INNER JOIN
titles As t On t.title_id = ta.title_id
ORDER BY au_lname, au_fname
回答by Tassadaque
General syntax for select statement is
select 语句的一般语法是
SELECT select_list
[INTO new_table_]
FROM table_source
[WHERE search_condition]
[GROUP BY group_by_expression]
[HAVING search_condition]
[ORDER BY order_expression [ASC | DESC] ;
so in your case
所以在你的情况下
SELECT au_lname, au_fname, t.title_id
from authors As a INNER JOIN
titleauthor As ta On a.au_id = ta.au_id INNER JOIN
titles As t On t.title_id = ta.title_id
GROUP BY au_lname
ORDER BY au_lname, au_fname