13

我想知道我是否可以在这样的列中使用 EXISTS (或类似的东西):

SELECT Column1,
       Column2,
       EXISTS (SELECT 1 FROM Table2 T2 WHERE T2.Column = T1.Column) AS IsFlag
FROM Table1

我知道我可以用 Count() 做类似的事情

SELECT Column1,
       Column2,
       (SELECT Count(*) FROM Table2 T2 WHERE T2.Column = T1.Column) AS IsFlag
FROM Table1

但是当 Table2 很大时,这可能不是很有效

4

2 回答 2

20

试试这个

SELECT Column1,
       Column2,
       CASE WHEN EXISTS (SELECT 1 FROM Table2 T2 
       WHERE T2.Column = T1.Column) then 1 ELSE 0 END AS IsFlag
FROM Table1
于 2012-10-09T19:50:33.303 回答
8
CASE 
    WHEN 
        EXISTS (SELECT 1 FROM Table2 T2 WHERE T2.Column = T1.Column) 
    THEN 1 
    ELSE 0 
END AS IsFlag
于 2012-10-09T19:50:55.453 回答