当前位置:Gxlcms > mysql > MySQL实现差集(Minus)和交集(Intersect)测试报告

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

时间:2021-07-01 10:21:17 帮助过:47人阅读

 可以用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实现交集

 

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实现差集

 

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中的int(m)
  • Mysql错误1366 - Incorrect integer value解决方法
  • MySQL中replace into语句的用法详解
  • mysql中TINYINT的取值范围
  • MySql中使用INSERT INTO语句更新多条数据的例子
  • Mysql错误Cannot find or open table x/x from the internal问题解决方法
  • 浅析MySQL replace into 的用法
  • mysql int(3)与int(11)的区别详解

人气教程排行