0

(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
  • SQL / Programming should be moved to Stack Overflow; this is not a systems-administration related topic, in my opinion. Commented Aug 6, 2010 at 2:10
  • possible duplicate of help with a sql query Commented Aug 7, 2010 at 2:17

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 [Orders].OrderId is redundant in the select clause - it is the same as [Order Details].OrderId
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.