If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Querying 3 tables at once..!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-14-09, 06:08
thunderstorm654 thunderstorm654 is offline
Registered User
 
Join Date: Apr 2009
Posts: 2
Querying 3 tables at once..!

Hi,I have three tables

order
Order_ID
OrderDate
Dispatched (either 0 or 1)
....

orderitem
OrderItem_ID
Order_ID
Qty
Product_ID

product
Product_ID
Name
Description
Price
...

I want to get a list of all the products within each order given a particular Order_ID.

So say the Order_ID is 35, I want to return for each order item within it the qty, product name,description and price.

At the moment I've tried

Code:
SELECT
   orderitem.Qty,
   product.Name AS ProductName,
   product.Description AS ProductDescription,
   product.Price
FROM
   order INNER JOIN orderitem
       ON order.Order_ID = orderitem.Order_ID
   INNER JOIN product
       ON orderitem.Product_ID = product.Product_ID
WHERE
   order.Order_ID = 35;
But I'm just getting an error:
"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 INNER JOIN orderitem ON order.Order_ID = orderitem.Order_ID IN' at line 7"

Find this join stuff really hard, any ideas what I'm doing wrong here?

Thanks
Reply With Quote
  #2 (permalink)  
Old 04-14-09, 06:16
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
order is a reserved word, so you need to quote youre table name:
Code:
FROM
   "order" INNER JOIN orderitem
       ON "order".Order_ID = orderitem.Order_ID
   INNER JOIN product
       ON orderitem.Product_ID = product.Product_ID
WHERE
   "order".Order_ID = 35;
Reply With Quote
  #3 (permalink)  
Old 04-14-09, 06:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by thunderstorm654
Find this join stuff really hard, any ideas what I'm doing wrong here?
you understand joins quite well, your query is fine

the problem is that ORDER is a reserved word

change the name of your table

many database designers prefer to use plural table names, since the idea is that a table is not a thing, but rather, a set of rows

so orders would work nicely

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 04-14-09, 06:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
shammat, quoting with doublequotes will work in mysql only if the appropriate server setting is switched on

mysql likes to use the horrid backtick character -- `order` -- but whether doublequotes or backticks are used, you have to remember to do it everywhere, all the time

renaming the table is a more suitable solution

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 04-14-09, 07:55
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by r937
shammat, quoting with doublequotes will work in mysql only if the appropriate server setting is switched on
Are there really people out there using MySQL without the ANSI flag?

Of course you are right, I always forget that I'm running MySQL in a extremely un-common way...
Reply With Quote
  #6 (permalink)  
Old 04-14-09, 09:05
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,260
better to learn the lesson now on reserved words, and pay the pain of making the changes now rather than let a kludgy workaround remain in perpetuity
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #7 (permalink)  
Old 04-14-09, 11:58
thunderstorm654 thunderstorm654 is offline
Registered User
 
Join Date: Apr 2009
Posts: 2
thanks guys, turns out `order` worked;-), but think ill change it to order as that is indeed annoying and inconsistent- thanks
Reply With Quote
  #8 (permalink)  
Old 04-16-09, 06:39
saurav.prasad28 saurav.prasad28 is offline
Registered User
 
Join Date: Apr 2009
Posts: 9
Hi

I think this is helpful for u

SELECT
product.name,
product.description,
product.price,
orderitem.quantity
FROM
product JOIN orderitem
ON product.product_id = orderitem.product_id
JOIN orderitem
ON orderitem.order_id = order.order_id
WHERE
Order.order_id = 35;
Reply With Quote
  #9 (permalink)  
Old 04-16-09, 08:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by saurav.prasad28
I think this is helpful for u
it is not

that query will produce a syntax error on order.order_id

can you guess why?

because your query does not include the order table

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 04-16-09, 08:18
saurav.prasad28 saurav.prasad28 is offline
Registered User
 
Join Date: Apr 2009
Posts: 9
Thumbs up ya their is mistake thanks for inform me but now it is right

SELECT
product.name,
product.description,
product.price,
orderitem.quantity,order.order_id
FROM
product JOIN orderitem
ON product.product_id = orderitem.product_id
JOIN orderitem
ON orderitem.order_id = order.order_id
WHERE
Order.order_id = 35;
Reply With Quote
  #11 (permalink)  
Old 04-16-09, 08:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by saurav.prasad28
ya their is mistake thanks for inform me but now it is right
not, it is not right

keep trying, you'll eventually get it

by the way, you're not testing these queries, you're just writing them out of thin air, correct?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On