Harsha M V
Harsha M V

Reputation: 54979

MySQL Where Clause error

i have the following statement

$result = mysql_query("SELECT * from rests ORDER BY name asc WHERE flag = '1' LIMIT 0 , 20");

am getting the following error

Invalid query: 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 flag = '1' LIMIT 0 , 20' at line 1

am not sure where am going wrong :(

Upvotes: 1

Views: 749

Answers (5)

prodigitalson
prodigitalson

Reputation: 60413

ORDER BY comes after the WHERE and LIMIT at the end.

Upvotes: 2

staticsan
staticsan

Reputation: 30575

The ORDER BY clause must be after the WHERE clause. That's all it is.

Upvotes: 2

Joel Spolsky
Joel Spolsky

Reputation: 33697

WHERE goes before ORDER BY.

Upvotes: 1

Ives.me
Ives.me

Reputation: 2394

$result = mysql_query("SELECT * from rests  WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20");

Upvotes: 5

codaddict
codaddict

Reputation: 455410

You cannot have the ORDER BY clause before WHERE clause.

Refer to the MySQL select syntax for the correct order of various clauses.

Try this:

SELECT * from rests WHERE flag = '1' ORDER BY name asc LIMIT 0 , 20

Also Ordering in ascending is default, you may drop the asc.

Upvotes: 4

Related Questions