Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query: Can't order by column called "order"?

I am pulling a column in an existing script into my template files, and everything is working great.

The only problem is, that this script has a column called order, and every row then has a number in that column to show which should be at the top etc. If I set my query to "ORDER BY name" for example, everything works fine, but when I use "ORDER BY order", then I get a SQL error.
Can I not have a column called order? I can't change column name, because it's part of the script.

Is there a way around it?

This is the line in my SQL query:

SELECT * FROM categories WHERE hide = 0 ORDER BY order 
like image 540
Debrah Avatar asked Oct 08 '12 19:10

Debrah


2 Answers

order is a keyword in SQL. So if you wish to use a keyword as a name, use backtick characters around it:

SELECT * FROM categories WHERE hide = 0 ORDER BY `order`

Try that :)

like image 134
Jamesking56 Avatar answered Oct 04 '22 05:10

Jamesking56


If you are working with Postgres just use "column_name", e.g:

SELECT "order" FROM table_name WHERE "order" > 10 ORDER BY "order";
like image 30
K. Stopa Avatar answered Oct 04 '22 07:10

K. Stopa