0

我有两个表格和数据如下。

create table t1 (id int, name varchar(10));

create table t2 (id int, name varchar(10), t1id int);

insert into t1 values
(1,'value 1'),
(2,'value 2'),
(3,'value 3'),
(4,'value 3');

insert into t2 values
(1,'value 1',1),
(2,'value 2',1),
(3,'value 3',1),
(4,'value 3',2);

我想要的是来自 T1 (id) 但不在 T2 (t1id) 中的 id 列表。

输出将是

3 , 4 作为 t1.id (3,4) 在 T2 (t1id) 中不存在。

原始数据

4

2 回答 2

1
SELECT id
  FROM t1
 WHERE NOT EXISTS (SELECT NULL
                     FROM t2
                    WHERE t2.t1id = t1.id)
于 2012-10-06T10:55:05.787 回答
0

为什么我提出问题后总是得到解决方案?

select id FROM t1 WHERE id NOT IN (select distinct(t1id) FROM t2);

演示

于 2012-10-06T10:55:30.230 回答