且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取 T-SQL 中子查询的当前记录

更新时间:2023-01-29 11:17:53

在比较两个值之前,您想加入表(并根据 ID 分组),如下所示:

You want to join the tables (and group on ID) before you can compare the two values like this:

SELECT  dbo.Table_1.*,
    CASE WHEN COUNT(dbo.Table_2.ID) = 0 THEN
        0
    ELSE
        1
    END
    AS HasException
FROM         dbo.Table_1 LEFT OUTER JOIN
                      dbo.Table_2 ON dbo.Table_1.ID = dbo.Table_2.ID
GROUP BY dbo.Table_1.ID