tags:

views:

74

answers:

3
+1  Q: 

MySQL Query puzzle

I have a table with two fields (id and word). Can anyone enlighten me as to why:

$query0 = "SELECT * FROM words WHERE id IN ($treated_words) ";

is a valid query. But:

$query0 = "SELECT * FROM words ORDER BY id ASC WHERE id IN ($treated_words) ";

is not. Many thanks

+6  A: 

try to move the ORDER BY id ASC after the WHERE id IN ($treated_words) part.

Etan
+2  A: 

You can't have order_by before where statement.

MySQL works this way. it selects everything or certain fields from table, applies to filter (where) and than orders by id. so that is why you can't order selected data and then apply filters to it.

Mohamed
+9  A: 

Order of elements in a SQL query matters. The ORDER BY clause must appear after the WHERE clause.

Look to the MySql docs for syntax rules. You'll see something like

SELECT [ DISTINCT | ALL ]
  column_expression1, column_expression2, ....
  [ FROM from_clause ]
  [ WHERE where_expression ]
  [ GROUP BY expression1, expression2, .... ]
  [ HAVING having_expression ]
  [ ORDER BY order_column_expr1, order_column_expr2, .... ]
Michael Petrotta