Home > Database > Mysql Tutorial > MySQL实现差集(Minus)和交集(Intersect)

MySQL实现差集(Minus)和交集(Intersect)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:49:59
Original
1715 people have browsed it

MySQL没有实现Minus和Intersect功能,就像它也没有实现cube的功能一样。可以用SQL实现同样的功能,就是麻烦了点。

MySQL没有实现Minus和Intersect功能,就像它也没有实现cube的功能一样。
 
可以用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;

--------------------------------------分割线 --------------------------------------

Ubuntu 14.04下安装MySQL

《MySQL权威指南(原书第2版)》清晰中文扫描版 PDF

Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主从服务器

Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群

Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb

--------------------------------------分割线 --------------------------------------

本文永久更新链接地址:

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template