| |
|
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.
|
 |

04-14-09, 06:08
|
|
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 
|
|

04-14-09, 06:16
|
|
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;
|
|

04-14-09, 06:17
|
|
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

|
|

04-14-09, 06:20
|
|
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

|
|

04-14-09, 07:55
|
|
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...
|
|

04-14-09, 09:05
|
|
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
|
|

04-14-09, 11:58
|
|
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
|
|

04-16-09, 06:39
|
|
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;
|
|

04-16-09, 08:08
|
|
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

|
|

04-16-09, 08:18
|
|
Registered User
|
|
Join Date: Apr 2009
Posts: 9
|
|
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;
|
|

04-16-09, 08:38
|
|
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?

|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|