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
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 :)
If you are working with Postgres just use "column_name", e.g:
SELECT "order" FROM table_name WHERE "order" > 10 ORDER BY "order";
AS orderis a SQL keyword, you should escape it properly as an field identifier by using backticks:
SELECT ... FROM ... ORDER by `order`
Try using backticks:
SELECT * FROM `categories` WHERE `hide` = 0 ORDER BY `order`
ORDER is a reserved word in SQL. You can use a reserved word as a column name but you must surround it in backticks when referencing it. It's good practice to surround all your column names in backticks so you don't run into this issue.
Try using back ticks around the column name, that should do it.
From the manual:
A reserved word can be used as an identifier if you quote it.
So you can use it like this:
SELECT * FROM categories WHERE hide = 0 ORDER BY `order`
Worked for me with brackets. SELECT T.* FROM dbo.test AS T ORDER BY [T].[ORDER]
Related
This is my SQL so far and nothing is working.
$st = DB::getInstance()->query("SELECT * FROM users WHERE group = 1 ORDER BY joined ASC");
All I want is to Select * from my table users where group = 1 (order by...)
Problem is my group column is a int. but I cannn't retrieve any data from it. Even if I try
group = '1'
Is there any function/way to get through this issue?
THanks!
group is a reserved word in many SQL implementation. Try using users.group = 1 .
Oracle SQL reserved words : https://docs.oracle.com/database/121/SQLRF/ap_keywd001.htm#SQLRF55621
MySQL reserved words : http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html
MSSQL reserved words : http://msdn.microsoft.com/en-us/library/ms189822.aspx
Yes, stop using select *
When writing your query, enter all the column names (or alias them if you want them to be called something different). For eg:
select
userID,
nickname,
email as loginEmail
from
// ... etc etc
It also seems that you are using a reserved word group as a column name. You need to either backtick this (mySQL uses the ` character) to tell the database that you mean it as a column/table name and not an actual function like this:
select
userID,
nickname,
email as loginEmail,
`group`
from
sometable
where
`group`=1
In my MySQL database, I have a table called pages and it contains several columns, two of which are order (int), tab (int), and name (text). When I call a query as such
"SELECT * FROM pages WHERE tab = '$tid'"
it executes with no problem, returning all rows with the correct tab label. The problem I am having is when I execute with an ORDER BY :
"SELECT * FROM pages WHERE tab = '$tid' ORDER BY order ASC"
I get a return of false from the query. No errors either.
When I put the column order in single quotes ', the query works like it did before, but applies no order.
Why am I getting no return regardless of which column I try to sort by? How do I make my query sort to a column correctly?
ORDER is a reserved word in MySQL, so you should escape it with backticks ` if you want to use it as an identifier :
SELECT * FROM pages WHERE tab = '$tid' ORDER BY `order`
You are getting an error is not coming out. Try the following:
select * from pages order by `order`;
ORDER is a reserved word. If you not quote it properly the query will not run.
i am using ORDER BY in mysql SELECT query but i dont know ots not ordering the data.. if i use this query its showing the table but not ordering the data in ascending order
$result = mysql_query("SELECT *FROM learningmaterial ORDER BY 'order' ASC")or die(mysql_error());
but if i use
$result = mysql_query("SELECT *FROM learningmaterial ORDER BY order ASC")or die(mysql_error());
then it give error that the syntax of the query is not right...i've seen on various sites but i couldnot found anything unique in my code...i think its right,...please check the query and mend a solution. Thankx in advance :)
You need backticks, not single quotes (a):
... SELECT * FROM learningmaterial ORDER BY `order` ASC ...
By using single quotes, you're ordering the rows by a constant (each row gets the same constant) so effectively not ordering them at all.
By using a "naked" column name of order, you're confusing the SQL parser, since order is a reserved word.
(a): Of course, this problem goes away if you stop using reserved words as column names but I assume you did that for a reason (such as a bucket-load of programs already depending on the fact that the column is called order).
Myself, I tend not to use generic names for columns (such as order or date), preferring instead things that don't conflict with the language (such as order_num or start_date). That way I don't have to worry about escaping.
You are using SQL reserved keyword order as a column name so use back-ticks to escape...like this
SELECT * FROM learningmaterial ORDER BY `order` ASC
I would suggest you to change the columnn name
Reference For List Of Reserved Keywords
ORDER is a reserved sql syntax keyword. you cannot use it directly
SELECT *FROM learningmaterial ORDER BY `order` ASC
-------------------------------^---------
in second case
SELECT *FROM learningmaterial ORDER BY order ASC
---------------------------------^-------^--
//this is a sql error
it doesn't make any sense.
Since Order is a reserved word, you need to wrap them using backticks not single quotes.
SELECT * FROM `learningmaterial` ORDER BY `order` ASC
Here is my PHP Query, but it doesn't seem to order the results. Everything else works fine.
SELECT *
FROM `main`
WHERE `user_legacy` LIKE '%".$name."%'
ORDER BY 'user_legacy' DESC LIMIT ".$limit
You use backticks (') to quote column names in SQL, not the normal single quote (').
ORDER BY `user_legacy` DESC
Also, if you don't do any kind of vetting or your input, that query is potentially vulnerable to an SQL injection.
Don't quote the column name.
ORDER BY user_legacy
ORDER BY needs a column name, you're passing it a string. Lose the quotes.
I have two columns of data. One column is called "like" which holds either the number 1 or 0. The other column is called "object" which can hold any word. I want to count the number of rows where the number 1 in the "like" column coincides with a certain word in the "object" column.
I have tried
$numrow1 = mysql_query("SELECT * FROM tablename WHERE like = 1 AND object = '$object' ");
$numlikes = mysql_num_rows($numrow1);
but I got a syntax error. Any help would be appreciated.
LIKE is a sql string comparison operator, maybe even object. If you have a field with a reserved name, then you have to directly address it or escape it.
Reformat your SQL statement inside the mysql_query like this:
SELECT *
FROM tablename AS T
WHERE T.like = 1
AND T.object = '$object'
If you only want to get the number of rows using, count() is much more efficient:
SELECT count(*)
FROM tablename
WHERE `like` = 1
AND object = '$object'
You did not show us the error message, but I gues it's because like is a reserved word, and therefor you need to quote the column name (I would strongly recommend to use a different name to avoid those problems in the future).
If you are running MySQL in ANSI compliant mode, you can also use the standard double quotes to escape the column name:
SELECT count(*)
FROM tablename
WHERE "like" = 1
AND object = '$object'
I'm not sure, but object could be a reserved word as well, so that might need quoting too.