从MYSQL表中选择10+ num_rows

时间:2015-02-21 14:44:55

标签: php mysql

有人可以告诉我这个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()); 

它说:

  

您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在' WHERE having count(*)>附近使用正确的语法。 10 ORDER BY RAND()LIMIT 1'在第1行

2 个答案:

答案 0 :(得分:2)

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()); 

请教:

您应该考虑使用PDOprepared statements切换到mysqli_* with prepared statements,因为mysql_*函数已弃用。

答案 1 :(得分:1)

删除WHERE子句,您已经有HAVING子句。

所以,你的陈述必须是:

$p = mysql_query("SELECT name,subname FROM brands GROUP BY name HAVING count(*) > 10 ORDER BY RAND() LIMIT 1")or die(mysql_error());