简体   繁体   English

关键字组附近的语法不正确

[英]Incorrect Syntax near Keyword Group

Hello I'm writing an sql query But i am getting a syntax error on the line with the GROUP BY. 您好,我在写一个SQL查询,但是在GROUP BY的行上出现语法错误。 What can possibly be the problem. 可能是什么问题。

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

Place GROUP BY before ORDER BY. 将GROUP BY放在ORDER BY之前。 Also, all fields included in the select must either be in the GROUP BY statement, or be inside an aggregate function. 另外,select中包含的所有字段必须在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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM