SQL查询以获取不属于联接的行
我在桌子下面吃饭SQL查询以获取不属于联接的行,sql,mysql,Sql,Mysql,我在桌子下面吃饭 create table test(int id,int data1); create table test1(int id,int data2); insert into test values(1,1,); insert into test1 values(2,2); insert into test1 values(3,3); insert into test1 values(1,1); 现在我想要不参与join的test行。i、 我想要第2、2和3、3行。我希望能
create table test(int id,int data1);
create table test1(int id,int data2);
insert into test values(1,1,);
insert into test1 values(2,2);
insert into test1 values(3,3);
insert into test1 values(1,1);
现在我想要不参与join的test行。i、 我想要第2、2和3、3行。我希望能够在mysql中做到这一点
由于性能原因,我不想使用内部查询
谢谢您不使用子查询,即使是我喜欢的现有种类,您也需要进行左连接,并获取未连接的记录,如下所示:
select a.* from test1 a
left join test b on a.id = b.id and a.data2 = b.data1
where b.id IS NULL
使用左JOIN/IS为空:
。否则,不存在/不存在是更好的选择
使用不存在:
也许是工会的事 select * from test as a left outer join test1 as o on a.id = o.id union all select * from test as a right outer join test1 as o on a.id = o.id where a.id is null; 我假设你想要达到什么,如果独家加入。
忘记了文件末尾的括号不存在
SELECT t1.*
FROM TEST1 t1
WHERE NOT EXISTS(SELECT NULL
FROM TEST t
WHERE t.id = t1.id
AND t.data1 = t1.data2)
select * from test as a
left outer join test1 as o on a.id = o.id
union all
select * from test as a
right outer join test1 as o on a.id = o.id
where a.id is null;