MySQL完成差集(Minus)和交集(Intersect)測試報告。本站提示廣大學習愛好者:(MySQL完成差集(Minus)和交集(Intersect)測試報告)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL完成差集(Minus)和交集(Intersect)測試報告正文
可以用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;