我想合併兩個SQL從一個表如何合併兩個SQL SELECT
查詢1選擇
SELECT ShoppingLike.WhichId
FROM ShoppingLike
WHERE (ShoppingLike.ShoppingScoreTypeId = 2) AND (ShoppingLike.UserId = 189)
代替75
查詢1如何我應該使用查詢2的結果嗎?
我想合併兩個SQL從一個表如何合併兩個SQL SELECT
查詢1選擇
SELECT ShoppingLike.WhichId
FROM ShoppingLike
WHERE (ShoppingLike.ShoppingScoreTypeId = 2) AND (ShoppingLike.UserId = 189)
代替75
查詢1如何我應該使用查詢2的結果嗎?
可能帶有子選擇查詢?
SELECT UserId
FROM ShoppingLike
WHERE WhichId IN (
SELECT WhichId
FROM ShoppingLike
WHERE (ShoppingScoreTypeId = 2) AND (UserId = 189)
)
儘管這將始終產生與UserId
189作爲結果行,在這種情況下,查詢可以簡單地這樣寫的:
SELECT UserId
FROM ShoppingLike
WHERE (ShoppingScoreTypeId = 2) AND (UserId = 189)
這將返回一個排的每個記錄與ShoppingScoreTypeId
= 2 UserId
189.
謝謝,它工作正常:) – dan
SELECT UserId
FROM ShoppingLike
WHERE (ShoppingScoreTypeId = 2)
AND WhichId = (
SELECT top 1 ShoppingLike.WhichId
FROM ShoppingLike
WHERE (ShoppingLike.ShoppingScoreTypeId = 2) AND (ShoppingLike.UserId = 189)
)
爲什麼「SELECT top 1」,它必須是數組的記錄,而不是隻有一條記錄 – dan
哦,你要求匹配你的第二個查詢的結果。在這種情況下,刪除「頂部1」,並更改「和WhereId(...) –
它可能會幫助,如果你提供樣本數據和預期的結果... – sgeddes
@sgeddes我猜他想要一個'聯合'...還能有什麼? –
@TimBiegeleisen - 不太確定的是,'union'可能有意義,但'exists'或'in'也可以。誰知道在這一點上...... – sgeddes