By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,316 Members | 2,355 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,316 IT Pros & Developers. It's quick & easy.

sql statement writing

100+
P: 553
Hi

I have following SQL Queries. Is there a way to to write a single query instead somehow instead?

-Get OrderID from this query
SELECT OrderID FROM Orders WHERE CustomerID='" + reader.Item("CustomerID")


-Using OrderID, get ProductID
"SELECT ProductID FROM OrderDetails WHERE OrderID=" + readerp.Item("OrderID")

-Using ProductID, get Products
SELECT ProductName FROM Products WHERE ProductID=" + reader1.Item("ProductID")

My question is instead of quering three times, can i write a single SQL statemnt to get the final result - (ProductName)

Thanks
Qi
Jun 30 '09 #1

✓ answered by ajalwaysus

This should do exactly what you want:

SELECT Orders.OrderID, OrderDetails.ProductID, Products.ProductName
FROM (OrderDetails INNER JOIN Orders ON OrderDetails.OrderID = Orders.OrderID) INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID;

Hope it helps,
AJ

Share this Question
Share on Google+
2 Replies


P: 36
Hi

You should be able to join all 3 tables and use WHERE for all conditions. Is that not working for you? (Subqueries are an alternative)

I hope it helps
Emil
Jul 1 '09 #2

Expert 100+
P: 266
This should do exactly what you want:

SELECT Orders.OrderID, OrderDetails.ProductID, Products.ProductName
FROM (OrderDetails INNER JOIN Orders ON OrderDetails.OrderID = Orders.OrderID) INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID;

Hope it helps,
AJ
Jul 17 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.