Solved

mysql left join

Posted on 2015-02-19
14
126 Views
Last Modified: 2015-02-20
Not sure why I am getting this error. I have done plenty of left joins before. Maybe I need a fresh set of eyes.

SELECT
	`orders`.`itemOptions`,
	`products`.`product_id`,
	`products`.`product_number`,
	`products`.`title`,
	`products`.`price`
		
FROM
	`orders`,`products`

LEFT JOIN `options` on `orders`.`itemOptions` = `options`.`optionID`

WHERE
	`orders`.`orderID` = '113'
	AND `orders`.`itemsOrdered` = `products`.`product_id`

Open in new window


Error: #1054 - Unknown column 'orders.itemOptions' in 'on clause'

Schema:

CREATE TABLE IF NOT EXISTS `options` (
  `optionID` int(11) NOT NULL AUTO_INCREMENT,
  `name` text NOT NULL,
  PRIMARY KEY (`optionID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

CREATE TABLE IF NOT EXISTS `products` (
  `product_id` int(250) NOT NULL AUTO_INCREMENT,
  `cat_id` varchar(250) NOT NULL,
  `product_number` varchar(50) DEFAULT NULL,
  `title` varchar(250) NOT NULL,
  `text` text,
  `photo` varchar(250) DEFAULT NULL,
  `price` decimal(5,2) DEFAULT NULL,
  `options` text,
  `online` int(11) DEFAULT '0',
  `order` int(11) DEFAULT '0',
  `noShipping` int(11) DEFAULT '0',
  `noTax` int(11) DEFAULT '0',
  PRIMARY KEY (`product_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=101 ;

CREATE TABLE IF NOT EXISTS `orders` (
  `orderID` int(11) NOT NULL AUTO_INCREMENT,
  `f_name` varchar(30) DEFAULT '',
  `l_name` varchar(30) DEFAULT '',
  `email` text,
  `phone` varchar(20) DEFAULT '',
  `address` text,
  `address2` text,
  `city` varchar(30) DEFAULT '',
  `state` varchar(30) DEFAULT '',
  `zip` varchar(20) DEFAULT '',
  `s_address` text,
  `s_address2` text,
  `s_city` varchar(30) DEFAULT '',
  `s_state` varchar(30) DEFAULT '',
  `s_zip` varchar(20) DEFAULT '',
  `comments` text,
  `ship_comments` text,
  `cardName` text,
  `cardType` varchar(20) DEFAULT '',
  `cardNumber` varchar(30) DEFAULT '',
  `exp` varchar(20) DEFAULT '',
  `cvv` varchar(10) DEFAULT '',
  `costShipping` double DEFAULT NULL,
  `costTax` double DEFAULT NULL,
  `costDiscount` double DEFAULT NULL,
  `costTotal` double DEFAULT NULL,
  `itemsOrdered` text,
  `itemOptions` varchar(200) DEFAULT NULL,
  `itemsQuantity` text,
  `dateAdded` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`orderID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=122 ;
0
Comment
Question by:Robert Saylor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
14 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40620412
In `orders`.`itemOptions` = `options`.`optionID`, one is an int and the other is a 'varchar'.  I don't think that will work.  I think you can only compare items of the same type.
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40620469
Seems like maybe there's a typo in there somewhere. Simplify it down to using table aliases and lose the backticks:

SELECT
      o.itemOptions,
      p.product_id,
      p.product_number,
      p.title,
      p.price
FROM
      orders o,
      products p
LEFT JOIN options op ON o.itemOptions = op.optionID
WHERE
      o.orderID = 113
      AND o.itemsOrdered = p.product_id

I say this because you are already selecting orders.itemOptions in your SELECT statement, so if the error says the column doesn't exist, then it would have thrown the error on the SELECT portion first. So it sounds like there's some bad whitespace or something in there somewhere.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40620481
o.itemOptions = op.optionID will never match because they are different 'types'.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40620606
please do not match JOIN with "non-join" syntax
also, please start to use aliases in your queries (as shown by the above comments)
http://www.experts-exchange.com/Database/Miscellaneous/A_11135-Why-should-I-use-aliases-in-my-queries.html

SELECT
	o.`itemOptions`,
	p.`product_id`,
	p.`product_number`,
	p.`title`,
	p.`price`
FROM 	`orders` o
JOIN `products` p
   ON  o.`itemsOrdered` =  p.`product_id`
LEFT JOIN `options` op on  o.`itemOptions` =  op.`optionID`
WHERE 	o.`orderID` = '113'
                                  

Open in new window

0
 
LVL 7

Author Comment

by:Robert Saylor
ID: 40620992
Thanks, I understand the issue now with the data type. I will most likely have to write it as a nested query now but that is life.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40621060
I agree that this may be some issue, but not the explanation to the error provided:

Error: #1054 - Unknown column 'orders.itemOptions' in 'on clause'
0
 
LVL 7

Author Comment

by:Robert Saylor
ID: 40621185
This should be closed or deleted. I did not get a solid answer to my question.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40621200
how did you solve your issue, then?
0
 
LVL 7

Author Comment

by:Robert Saylor
ID: 40621222
Guy I did not solve it. I wish my question to be removed.
0
 
LVL 7

Author Comment

by:Robert Saylor
ID: 40621278
I've requested that this question be deleted for the following reason:

I did not get the answer I need and I will have to re-write this query.
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40621276
Please do not close the question with a simple "re-write" self solution. Part of this whole process is that someone else will come along with the same question and if they find your question and answer, it will help them out. If you delete it, that help goes away with all of the above effort.

Dave's original answer #40620412 is 100% correct. I tried it out on a local test database and that is indeed the resulting error message (a bizarre error message for that problem, but it's true). So please accept his answer as the solution and then you can re-write the query.
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40621279
Sorry, that should have been an objection.
0
 
LVL 7

Author Comment

by:Robert Saylor
ID: 40621309
Please let this close GUY ok. Like I said the first time I accept this as the issue.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question