2016-07-08 54 views
0

我想合併兩個SQL從一個表如何合併兩個SQL SELECT

查詢1選擇

SELECT ShoppingLike.WhichId 
FROM ShoppingLike 
WHERE (ShoppingLike.ShoppingScoreTypeId = 2) AND (ShoppingLike.UserId = 189) 

代替75查詢1如何我應該使用查詢2的結果嗎?

+1

它可能會幫助,如果你提供樣本數據和預期的結果... – sgeddes

+0

@sgeddes我猜他想要一個'聯合'...還能有什麼? –

+0

@TimBiegeleisen - 不太確定的是,'union'可能有意義,但'exists'或'in'也可以。誰知道在這一點上...... – sgeddes

回答

0

可能帶有子選擇查詢?

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.

+0

謝謝,它工作正常:) – dan

0
SELECT  UserId 
FROM   ShoppingLike 
WHERE  (ShoppingScoreTypeId = 2) 
AND WhichId = (
SELECT  top 1 ShoppingLike.WhichId 
FROM   ShoppingLike 
WHERE  (ShoppingLike.ShoppingScoreTypeId = 2) AND (ShoppingLike.UserId = 189) 
) 
+0

爲什麼「SELECT top 1」,它必須是數組的記錄,而不是隻有一條記錄 – dan

+0

哦,你要求匹配你的第二個查詢的結果。在這種情況下,刪除「頂部1」,並更改「和WhereId(...) –