欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL实现差集(Minus)和交集(Intersect)测试报告

程序员文章站 2024-02-28 16:18:58
 可以用sql实现同样的功能,就是麻烦了点。 drop table t1; drop table t2; create table t1...

 可以用sql实现同样的功能,就是麻烦了点。

 drop table t1;

 drop table t2;

create table t1(id int primary key,nickname varchar(20),playnum varchar(20));

create table t2(id int primary key,nickname varchar(20),playnum varchar(20));

insert into t1 values(1,1,10);

insert into t1 values(2,2,20);

insert into t1 values(3,3,30);

insert into t2 values(1,1,10);

insert into t2 values(2,2,200);

insert into t2 values(3,33,300);

commit;

MySQL实现差集(Minus)和交集(Intersect)测试报告

mysql实现交集

 

select id, nickname, playnum, count(*)

 from (select id, nickname, playnum

from t1

union all

select id, nickname, playnum

from t2

) a

group by id, nickname, playnum

having count(*) > 1

MySQL实现差集(Minus)和交集(Intersect)测试报告

mysql实现差集

 

select t1.id, t1.nickname, t1.playnum

 from t1 left join t2 on t1.id = t2.id

where t1.nickname != t2.nickname

or t1.playnum != t2.playnum;

MySQL实现差集(Minus)和交集(Intersect)测试报告