错误是
'where' 子句中的未知列 'num'
SELECT COUNT(*) AS num, books_bookid
FROM bookgenre_has_books
WHERE num > 10
GROUP BY books_bookid
我究竟做错了什么?谢谢。
WHERE
子句看不到别名,使用HAVING
.
不允许在 WHERE 子句中引用列别名,因为在执行 WHERE 子句时可能尚未确定列值
http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html
试试这个,你应该使用 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 返回的记录。
我们可以这样写
SELECT COUNT(*) , books_bookid
FROM bookgenre_has_books
GROUP BY books_bookid
having count(*) > 10
您正在检查重复项而不是该列books_bookid
尝试这个
`SELECT COUNT(*) , books_bookid
FROM bookgenre_has_books
GROUP BY books_bookid
having count(*) > 10`