我想知道是否可以在列中使用EXISTS(或类似的东西):
SELECT Column1,Column2,EXISTS (SELECT 1 FROM Table2 T2 WHERE T2.Column = T1.Column) AS IsFlag FROM Table1
我知道我可以做一些类似Count()
SELECT Column1,(SELECT Count(*) FROM Table2 T2 WHERE T2.Column = T1.Column) AS IsFlag FROM Table1
但是,如果Table2很大,那可能不是非常有效
解决方法
尝试这个
SELECT Column1,CASE WHEN EXISTS (SELECT 1 FROM Table2 T2 WHERE T2.Column = T1.Column) then 1 ELSE 0 END AS IsFlag FROM Table1