有人可以告诉我这个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行
答案 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());
请教:
您应该考虑使用PDO或prepared 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());