Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query error for query containing a column named "order"

For the life of me i can seem to figure it out

INSERT INTO category SET CategoryName = 'Hardware_1',
Category = 'HARDWARE', Status = '1', Order = '1'

 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 'Order = '1'' at line 1

CREATE TABLE `category` (
  `CategoryID` int(11) NOT NULL AUTO_INCREMENT,
  `CategoryName` varchar(255) NOT NULL,
  `Category` varchar(255) NOT NULL,
  `Status` tinyint(4) NOT NULL,
  `Order` int(11) NOT NULL,
  PRIMARY KEY (`CategoryID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
like image 850
Matt Elhotiby Avatar asked Nov 10 '10 22:11

Matt Elhotiby


People also ask

Can you query columns in any order?

The order doesn't matter, actually, so you are free to order them however you'd like. edit: I guess a bit more background is helpful: As far as I know, the process of optimizing any query happens prior to determining exactly what subset of the row data is being pulled.

How can you fix an ambiguous column reference error?

This means two columns have the same column name — that is the “Name” column. The SQL Machine is confused as to which “Name” out of the two tables you are referring to. It is ambiguous — not clear. To clarify this, add the alias of either or both TABLE1 or TABLE2 to the columns having the same name.


2 Answers

Order is a reserved word. Enclose Order in backticks if you intend to use it.

INSERT INTO category SET CategoryName = 'Hardware_1',
Category = 'HARDWARE', Status = '1', `Order` = '1'
like image 165
Buggabill Avatar answered Sep 30 '22 15:09

Buggabill


As Cfreak pointed out in the comments, your syntax is valid. It's your use of the unescaped Order keyword that is the issue.

Insert Into category (CategoryName, Category, Status, `Order`)
Values ('Hardware_1', 'HARDWARE', '1', '1')
like image 41
Brandon Avatar answered Sep 30 '22 14:09

Brandon