首頁 >資料庫 >mysql教程 >MySQL实现差集(Minus)和交集(Intersect)测试报告_MySQL

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

WBOY
WBOY原創
2016-06-01 13:07:551081瀏覽

 可以用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, playNumFROM t1UNION ALLSELECT id, nickname, playNumFROM t2) aGROUP BY id, nickname, playNumHAVING COUNT(*) > 1

MySQL实现差集

 

SELECT t1.id, t1.nickname, t1.playNum FROM t1 LEFT JOIN t2 ON t1.id = t2.idWHERE t1.nickname != t2.nicknameOR t1.playNum != t2.playNum;

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn