dan dan - 5 months ago 7
SQL Question

How to merge two sql select

I want to merge two sql select from one table
Query1:

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


Query2:

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


Instead of 75 on Query1 how should I have result of Query2

Answer

Possibly with a sub-select query?

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

Although this would always produce rows with UserId 189 as the result, in which case the query could simply be written like this:

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

This would return a row for each record with ShoppingScoreTypeId = 2 for UserId 189.