MySQL Where Clause错误

时间:2010-12-09 05:57:55

标签: php mysql where-clause

我有以下声明

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

我收到以下错误

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

我不确定哪里出错了:(

5 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:4)

ORDER BY子句之前不能有WHERE子句。

请参阅MySQL select syntax了解各种条款的正确顺序。

试试这个:

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

另外,以升序排序是默认的,您可以放弃asc

答案 2 :(得分:2)

ORDER BY子句必须在WHERE子句之后。就是这样。

答案 3 :(得分:2)

ORDER BY位于WHERELIMIT之后。

答案 4 :(得分:1)

WHERE早于ORDER BY