Sql server 选择具有相同ID的行,然后选择剩余的不具有相同ID的行

Sql server 选择具有相同ID的行,然后选择剩余的不具有相同ID的行,sql-server,tsql,select,Sql Server,Tsql,Select,我需要先选择具有公共id的行,然后选择没有公共id的其余行(无重复) 这就是我到目前为止所做的: SELECT DISTINCT a.cars,b.wheels,c.glass FROM table auto a, tires b, window c WHERE a.ID = b.ID AND c.ID = a.ID ????AND/OR a.ID <> b.ID???? 轮胎 id wheels 1 data 2 data 5 data <-- ID diff

我需要先选择具有公共id的行,然后选择没有公共id的其余行(无重复)

这就是我到目前为止所做的:

SELECT DISTINCT a.cars,b.wheels,c.glass
FROM table auto a, tires b, window c
WHERE a.ID = b.ID AND c.ID = a.ID
????AND/OR a.ID <> b.ID????
轮胎

id wheels
1   data
2   data
5   data  <-- ID different from table 'Auto' but still want to select it
9   data  <-- ID different from table 'Auto' but still want to select it
200 data  <-- ID different from table 'Auto' but still want to select it

使用左连接(而不是通过WHERE子句进行的隐式内部连接)将从
auto
表中返回匹配行和不匹配行

SELECT DISTINCT a.cars, b.wheels, c.glass
    FROM auto a
        LEFT JOIN tires b
            ON a.id = b.id
        LEFT JOIN window c
            ON a.id = c.id
    ORDER BY a.cars, b.wheels, c.glass

您需要向我们展示一些表结构和示例数据,并更清楚地解释您在这里试图实现的目标……天哪,您如何才能理解这个问题的真正含义!!?!?我佩服你的才华……:)@marc_s:将用户需求文档转换为功能代码的多年实践。:-)正如Joe所展示的,显式声明联接总是一个好主意。这有助于防止任何歧义,并防止您进行笛卡尔连接(如果您弄乱了WHERE子句)。
id glass
1  data
2  data
3  data
4  data
SELECT DISTINCT a.cars, b.wheels, c.glass
    FROM auto a
        LEFT JOIN tires b
            ON a.id = b.id
        LEFT JOIN window c
            ON a.id = c.id
    ORDER BY a.cars, b.wheels, c.glass