简体   繁体   中英

MySQL Query Error 1054

I have this MySQL query

SELECT count( * ) AS total
FROM `orders` o, `orders_status` s, `orders_status_history` osh
LEFT JOIN `orders_total` ot ON ot.orders_id = o.orders_id
WHERE o.orders_status = s.orders_status_id
AND osh.orders_id = o.orders_id
AND s.language_id = '5'
AND s.orders_status_id = '6'
AND ot.class = 'ot_total'

getting this error:

#1054 - Unknown column 'o.orders_id' in 'on clause'

My table schema is as follows:

CREATE TABLE IF NOT EXISTS `orders` (
  `orders_id` int(11) NOT NULL auto_increment,
  `customers_id` int(11) NOT NULL default '0',
  `last_modified` datetime default NULL,
  `orders_status` int(5) NOT NULL default '0',
  `orders_date_finished` datetime default NULL,
  `comments` text,
  `currency` char(3) default NULL,
  `currency_value` decimal(14,6) default NULL,
  `invoice_number` varchar(100) default NULL,
  PRIMARY KEY  (`orders_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=cp1251 AUTO_INCREMENT=8419 ;

And I can't understand where the error comes from. Any ideas?

You have an OR as your table alias for your orders table. You need to change the table alias name.

SELECT count( * ) AS total
FROM `orders` o , `orders_status` s, `orders_status_history` osh
LEFT JOIN `orders_total` ot ON ot.orders_id = or.orders_id
WHERE o.orders_status = s.orders_status_id
AND osh.orders_id = or.orders_id
AND s.language_id = '5'
AND s.orders_status_id = '6'
AND ot.class = 'ot_total'

You should also change your JOIN syntax

SELECT count( * ) AS total
FROM `orders` o 
LEFT JOIN `orders_status` s
    ON o.order_status = s.orders_status_id
LEFT JOIN `orders_status_history` osh
    ON osh.orders_id = o.orders_id  
LEFT JOIN `orders_total` ot 
    ON ot.orders_id = o.orders_id
WHERE s.language_id = '5'
    AND s.orders_status_id = '6'
    AND ot.class = 'ot_total'

As stated in the manual :

Previously, the comma operator ( , ) and JOIN both had the same precedence, so the join expression t1, t2 JOIN t3 was interpreted as ((t1, t2) JOIN t3) . Now JOIN has higher precedence, so the expression is interpreted as (t1, (t2 JOIN t3)) . This change affects statements that use an ON clause, because that clause can refer only to columns in the operands of the join, and the change in precedence changes interpretation of what those operands are.

Example:

 CREATE TABLE t1 (i1 INT, j1 INT); CREATE TABLE t2 (i2 INT, j2 INT); CREATE TABLE t3 (i3 INT, j3 INT); INSERT INTO t1 VALUES(1,1); INSERT INTO t2 VALUES(1,1); INSERT INTO t3 VALUES(1,1); SELECT * FROM t1, t2 JOIN t3 ON (t1.i1 = t3.i3); 

Previously, the SELECT was legal due to the implicit grouping of t1,t2 as (t1,t2) . Now the JOIN takes precedence, so the operands for the ON clause are t2 and t3 . Because t1.i1 is not a column in either of the operands, the result is an Unknown column 't1.i1' in 'on clause' error. To allow the join to be processed, group the first two tables explicitly with parentheses so that the operands for the ON clause are (t1,t2) and t3 :

 SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3); 

Alternatively, avoid the use of the comma operator and use JOIN instead:

 SELECT * FROM t1 JOIN t2 JOIN t3 ON (t1.i1 = t3.i3); 

This change also applies to statements that mix the comma operator with INNER JOIN , CROSS JOIN , LEFT JOIN , and RIGHT JOIN , all of which now have higher precedence than the comma operator.

Or is a mysql keyword! replace it with something else, eg _or & try again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM