有人能告诉我这个 mysql 查询有什么问题吗?
$p = mysql_query("SELECT name,subname FROM brands GROUP BY name WHERE
having count(*) > 10 ORDER BY RAND() LIMIT 1")or die(mysql_error());
它说:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE having count(*) > 10 ORDER BY RAND() LIMIT 1' at line 1
最佳答案
HAVING
不使用 WHERE
子句,将其删除。
$p = mysql_query("SELECT name,subname FROM brands GROUP BY name
having count(*) > 10 ORDER BY RAND() LIMIT 1")or die(mysql_error());
咨询:
- http://www.mysqltutorial.org/mysql-having.aspx
- http://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html
您应该考虑切换到 PDO与 prepared statements或 mysqli_*
with prepared statements ,因为 mysql_*
函数已弃用。
关于php - 从 MYSQL 表中选择 10+ num_rows 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28647051/