Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Noob Simple Query Help

Author  Topic 

jjwest
Starting Member

2 Posts

Posted - 2012-04-11 : 20:03:53
i have a query in which i have to return OrderID, OrderDate and ShipCity for all orders where the customers city is Toronto

The orders table has PK OrderID, OrderDate and ShipCity etc in it and has FK CustomerID and the Customer table has PK CustomerID City etc

This is the code i have come up with

Select OrderID, OrderDate, ShipCity
From Orders
WHERE Orders.CustomerID = Customer.CustomerID
AND Customer.City = ‘Toronto’

Any help would be appreciated

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-11 : 20:47:54
You have to also include Customer table in the FROM clause. I prefer to use explicit join statements rather than the old ANSI style joins, so your query would be something like this:
SELECT o.OrderID,
o.OrderDate,
o.ShipCity
FROM Orders o
INNER JOIN Customers c
ON o.CustomerID = c.CustomerID
WHERE c.Customer.City = 'Toronto'
Go to Top of Page

jjwest
Starting Member

2 Posts

Posted - 2012-04-11 : 20:54:13
thanks this way is alot clearer
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-04-12 : 19:40:39
You are welcome - but now that I am looking at it, I am surprised it worked; I assume you changed it to

WHERE c.Customer.City = 'Toronto'
Go to Top of Page
   

- Advertisement -