Unknown column error in this COUNT MySQL statement?

后端 未结 4 467
情书的邮戳
情书的邮戳 2021-01-20 01:11

Error is

Unknown column \'num\' in \'where\' clause

SELECT COUNT(*) AS num, books_bookid
FROM bookgenre_has_books
WHERE         


        
相关标签:
4条回答
  • 2021-01-20 01:26

    Try this, you should use the HAVING clause

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

    The SQL HAVING clause is used in combination with the SQL GROUP BY clause. It can be used in an SQL SELECT statement to filter the records that a SQL GROUP BY returns.

    0 讨论(0)
  • 2021-01-20 01:31

    Try this

    `SELECT COUNT(*) , books_bookid
     FROM bookgenre_has_books
     GROUP BY books_bookid
     having count(*) > 10`
    
    0 讨论(0)
  • 2021-01-20 01:39

    WHERE clause cant see aliases,use HAVING.

    It is not allowable to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed

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

    0 讨论(0)
  • 2021-01-20 01:45

    We can write like this

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

    you are checking for duplicates more then for that column books_bookid

    0 讨论(0)
提交回复
热议问题