1

我有桌子ABC。现在表A作为列A1(主键),表B有列B1(主键),表C有列A1(表的外键A.A1),B1(表的外键B.B1)。

现在我正在编写一个查询,其中列出了所有行AB一个位列,如果在表中找到匹配的行,则该列将设置为 1,C否则为 0。

SELECT 
     ISNULL((SELECT CAST(1 AS BIT) 
             FROM C 
             WHERE C.A1 = A.A1 AND C.B1 = B.B1),0) AS [TAG],
     A.A1,
     B.B1
FROM A CROSS JOIN B

Subquery returned more than 1 value.即使在组合A1和列后查询没有重复行,此查询也会产生错误B1

4

1 回答 1

2

试试这个,

SELECT  CAST(COALESCE(c.b1, 0) AS BIT)   TAG
FROM
        (
            SELECT  A1, B1
            FROM    A CROSS JOIN B
        ) D LEFT JOIN C
                ON  c.A1 = d.A1 AND
                    c.B1 = D.B1
于 2012-11-07T06:14:07.427 回答