2查询单独运行很好,但组合起来会产生语法错误

zphenhs4  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(282)

第一个查询涉及与特定主题相关的书籍和作者列表,并生成正确的结果:

SELECT BOOK_NUM,
BOOK_TITLE,
BOOK_SUBJECT,
AU_LNAME
FROM BOOK
JOIN WRITES USING (BOOK_NUM)
JOIN AUTHOR USING (AU_ID)
WHERE BOOK_SUBJECT = 'CLOUD'
ORDER BY BOOK_TITLE;

第二个扩展了主题,也产生了正确的结果:

SELECT BOOK_NUM,
BOOK_TITLE,
BOOK_SUBJECT,
AU_FNAME,
AU_LNAME,
Count(BOOK_TITLE) AS `NUM BOOKS BY AUTHOR`
FROM BOOK
JOIN WRITES USING (BOOK_NUM)
JOIN AUTHOR USING (AU_ID)
GROUP BY BOOK_NUM, AU_ID
ORDER BY `NUM BOOKS BY AUTHOR`,
AU_LNAME;

当我将两者结合起来时,会在book\u subject='cloud'处产生语法错误:

SELECT BOOK_NUM,
BOOK_TITLE,
BOOK_SUBJECT,
AU_FNAME,
AU_LNAME,
Count(BOOK_TITLE) AS `NUM BOOKS BY AUTHOR`
FROM BOOK
JOIN WRITES USING (BOOK_NUM)
JOIN AUTHOR USING (AU_ID)
GROUP BY BOOK_NUM, AU_ID
WHERE BOOK_SUBJECT = 'CLOUD'
ORDER BY `NUM BOOKS BY AUTHOR`, 
AU_LNAME,
BOOK_TITLE;

不管我把它放在哪里,我都会得到同样的错误,如果我删除它,代码就会运行。我需要怎么做才能解决这个问题?我需要把这个where子句包括在内。事先谢谢你帮我。

9rnv2umw

9rnv2umw1#

问得好。试着参考一些关于表达式顺序的文档。。。
请尝试以下代码,我们将 WHERE 适当地陈述。

SELECT 
BOOK_NUM,
BOOK_TITLE,
BOOK_SUBJECT,
AU_FNAME,
AU_LNAME,
Count(BOOK_TITLE) AS `NUM BOOKS BY AUTHOR`

FROM BOOK
JOIN WRITES USING (BOOK_NUM)
JOIN AUTHOR USING (AU_ID)

WHERE BOOK_SUBJECT = 'CLOUD'

GROUP BY BOOK_NUM, AU_ID

ORDER BY `NUM BOOKS BY AUTHOR`, 
AU_LNAME,
BOOK_TITLE;
xqk2d5yq

xqk2d5yq2#

不确定您使用的是什么sql。尝试将where子句移到group by上方。

相关问题