deniz
deniz

Reputation: 189

Selecting 10+ num_rows results from MYSQL table

could someone tell me whats wrong on this mysql query?

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

it says:

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

Upvotes: 1

Views: 65

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

HAVING does not use a WHERE clause, remove it.

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

Consult:

You should consider switching to PDO with prepared statements or mysqli_* with prepared statements, as the mysql_* functions are deprecated.

Upvotes: 2

shauryachats
shauryachats

Reputation: 10385

Remove the WHERE clause, you already have a HAVING clause.

So, your statement must be :

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

Upvotes: 1

Related Questions