T-SQL作为列存在

时间:2022-05-07 09:05:44

I'm wondering whether I can use EXISTS (or something similar) in column like this:

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

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

I know I can do something similar with Count()

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

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

But that might not be very efficient when Table2 is large

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

2 个解决方案

#1


16  

Try this

试试这个

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

#2


7  

CASE 
    WHEN 
        EXISTS (SELECT 1 FROM Table2 T2 WHERE T2.Column = T1.Column) 
    THEN 1 
    ELSE 0 
END AS IsFlag

#1


16  

Try this

试试这个

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

#2


7  

CASE 
    WHEN 
        EXISTS (SELECT 1 FROM Table2 T2 WHERE T2.Column = T1.Column) 
    THEN 1 
    ELSE 0 
END AS IsFlag