5

错误是

'where' 子句中的未知列 'num'

SELECT COUNT(*) AS num, books_bookid
FROM bookgenre_has_books
WHERE num > 10
GROUP BY books_bookid

我究竟做错了什么?谢谢。

4

4 回答 4

11

WHERE子句看不到别名,使用HAVING.

不允许在 WHERE 子句中引用列别名,因为在执行 WHERE 子句时可能尚未确定列值

http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html

于 2013-10-21T18:48:18.293 回答
3

试试这个,你应该使用 HAVING 子句

SELECT COUNT(*) AS num, books_bookid
FROM bookgenre_has_books
GROUP BY books_bookid
HAVING COUNT(*) > 10

SQL HAVING 子句与 SQL GROUP BY 子句结合使用。它可以在 SQL SELECT 语句中用于过滤 SQL GROUP BY 返回的记录。

于 2013-10-21T18:49:06.823 回答
1

我们可以这样写

    SELECT COUNT(*) , books_bookid
   FROM bookgenre_has_books
   GROUP BY books_bookid
   having count(*) > 10

您正在检查重复项而不是该列books_bookid

于 2013-10-21T18:50:54.190 回答
1

尝试这个

`SELECT COUNT(*) , books_bookid
 FROM bookgenre_has_books
 GROUP BY books_bookid
 having count(*) > 10`
于 2016-09-29T08:24:58.720 回答