tags:

views:

16

answers:

1

(newbie, help if you can please)

hi, i could use some help writing a SQL query. i'm trying to display some data from one table but the data i need depends on a value from a different table. i'm pretty new to this so i will try to explain this the best i can:

i have an 'Orders' table with a 'ShipCity' and 'OrderId' columns. i would like to get the 'OrderId' value from 'Orders' where 'ShipCity' = 'Caracas'. using those distinct 'OrderId' values, i would like to query a different table called 'Order Details' where [Order Details].[OrderId] = [Orders].[OrderId] (= to 'Caracas').

i hope that made sense. where i'm getting stuck is i'm sure that i will either need to create some variables or a temporary table to store these values and i don't have any experience with these things yet. i'd appreciate any help. also, these are tables in the Northwind sample database if that helps. below is a dummy sample of what i'm trying to do.

Select OrderId
FROM [Orders]
WHERE ShipCity = 'Caracas'

Select OrderId
FROM [Order Details]
WHERE OrderId = (Orders.ShipCity = 'Caracas')

here's another way of looking at it:

SELECT OrderId
FROM [Order Details]
WHERE OrderId = [Orders].ShipCity = 'Caracas'
+2  A: 

I think that your question is slightly confusing, but what I think you're asking is for a way to select records from [Order Details] where [Orders].[ShipCity] = 'Caracas'. If that is the case:

SELECT [Orders].OrderId, [Order Details].*
FROM [Orders] o
INNER JOIN [Order Details] od ON o.OrderId = od.OrderId
WHERE o.ShipCity = 'Caracas'

Also, this question should be moved to Stack Overflow.

Stemen
+1 `[Orders].OrderId` is redundant in the select clause - it is the same as `[Order Details].OrderId`
Mark Bannister
I agree that it is redundant, but I intentionally included [Orders].OrderId in the select list so that hopefully he would see the duplication, or if he immediately changed [Order Details].* to something else, that the OrderId would still be there.
Stemen