sql statement writing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • questionit
    Contributor
    • Feb 2007
    • 553

    sql statement writing

    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("Cu stomerID")


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

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

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

    Thanks
    Qi
  • IT Couple
    New Member
    • May 2009
    • 36

    #2
    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

    Comment

    • ajalwaysus
      Recognized Expert Contributor
      • Jul 2009
      • 266

      #3
      This should do exactly what you want:

      SELECT Orders.OrderID, OrderDetails.Pr oductID, Products.Produc tName
      FROM (OrderDetails INNER JOIN Orders ON OrderDetails.Or derID = Orders.OrderID) INNER JOIN Products ON OrderDetails.Pr oductID = Products.Produc tID;

      Hope it helps,
      AJ

      Comment

      Working...