开发者

use a sql select statement to get parameters for 2nd select statement

I am trying to write a sql statement that

I have 2 tables Store & StoreTransactions. My first select command looks like

SELECT [StoreID],[ParentStoreID] 
FROM Store

Very simple stuff. How do I take the returned StoreID's and use them for my 2nd select statement?

SELECT [StoreTransactionID],[TransactionDat开发者_如何学JAVAe],[StoreID]
FROM StoreTransactions
WHERE StoreID = returned values from the above query

Any help would be great!


SELECT [StoreTransactionID],[TransactionDate],[StoreID]
FROM StoreTransactions
WHERE StoreID in (select StoreId from Store)

This is known as a nested select, or inner select.


Couple of other ways of doing it...

SELECT [StoreTransactionID],[TransactionDate],[StoreID]
FROM StoreTransactions st
WHERE EXISTS
(
SELECT * 
FROM Store s
WHERE s.[StoreID] = st.[StoreID]
)

And

SELECT [StoreTransactionID],[TransactionDate],st.[StoreID]
FROM StoreTransactions st
INNER JOIN Store s ON s.[StoreID] = st.[StoreID]


An alternative way of writing it is to use an INNER JOIN

SELECT [StoreTransactionID],[TransactionDate],[StoreTransactions.StoreID]
FROM StoreTransactions INNER JOIN Store ON StoreTransactions.StoreID=Store.StoreID

This may be more efficient in some RDBMSs.

If your Store query also includes a WHERE clause, you can just add that to the query above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜