mysql中怎么查询差集 mysql 集合差
mysql两表汇总后再得到两表的交集和差集,请问怎么实现?
CREATE TABLE A (
成都创新互联公司主要从事成都网站设计、成都做网站、外贸网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务钢城,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
`C1` VARCHAR(10),
`C2` VARCHAR(10),
`C3` VARCHAR(10),
`C4` VARCHAR(10),
`C5` INT
);
INSERT INTO A
SELECT '华北', '辽宁', '沈阳', '20020202', 2 UNION ALL
SELECT '华东', '山东', '青岛', '18020133', 3 UNION ALL
SELECT '华北', '北京', '北京', '7193', 7 UNION ALL
SELECT '华北', '北京', '北京', '8358', 5 UNION ALL
SELECT '华北', '北京', '北京', '9773', 8 UNION ALL
SELECT '华东', '山东', '青岛', '18020133', 5 ;
CREATE TABLE B (
`C1` VARCHAR(10),
`C2` VARCHAR(10),
`C3` VARCHAR(10),
`C4` VARCHAR(10),
`C5` INT
);
INSERT INTO B
SELECT '华北', '辽宁', '沈阳', '20020202', 5 UNION ALL
SELECT '华东', '山东', '青岛', '18020133', 6 UNION ALL
SELECT '华北', '北京', '北京', '7193', 2 UNION ALL
SELECT '华东', '甘肃', '兰州', '7364', 3 UNION ALL
SELECT '华东', '湖北', '武汉', '7512', 7 UNION ALL
SELECT '华东', '湖北', '武汉', '7512', 2 UNION ALL
SELECT '华东', '江苏', '常州', '7582', 5 UNION ALL
SELECT '华东', '江苏', '淮安', '7670', 7;
SELECT
IFNULL(A.`C1`, B.`C1`) AS `区域`,
IFNULL(A.`C2`, B.`C2`) AS `省份`,
IFNULL(A.`C3`, B.`C3`) AS `城市`,
IFNULL(A.`C4`, B.`C4`) AS `编码`,
SUM(A.`C5`) AS `表A销量`,
SUM(B.`C5`) AS `表B销量`
FROM
A LEFT JOIN B
ON (
A.`C1` = B.`C1` AND
A.`C2` = B.`C2` AND
A.`C3` = B.`C3` AND
A.`C4` = B.`C4`
)
GROUP BY
IFNULL(A.`C1`, B.`C1`),
IFNULL(A.`C2`, B.`C2`),
IFNULL(A.`C3`, B.`C3`),
IFNULL(A.`C4`, B.`C4`)
UNION
SELECT
IFNULL(A.`C1`, B.`C1`) AS `区域`,
IFNULL(A.`C2`, B.`C2`) AS `省份`,
IFNULL(A.`C3`, B.`C3`) AS `城市`,
IFNULL(A.`C4`, B.`C4`) AS `编码`,
SUM(A.`C5`) AS `表A销量`,
SUM(B.`C5`) AS `表B销量`
FROM
A RIGHT JOIN B
ON (
A.`C1` = B.`C1` AND
A.`C2` = B.`C2` AND
A.`C3` = B.`C3` AND
A.`C4` = B.`C4`
)
GROUP BY
IFNULL(A.`C1`, B.`C1`),
IFNULL(A.`C2`, B.`C2`),
IFNULL(A.`C3`, B.`C3`),
IFNULL(A.`C4`, B.`C4`);
+------+------+------+----------+---------+---------+
| 区域 | 省份 | 城市 | 编码 | 表A销量 | 表B销量 |
+------+------+------+----------+---------+---------+
| 华北 | 辽宁 | 沈阳 | 20020202 | 2 | 5 |
| 华北 | 北京 | 北京 | 7193 | 7 | 2 |
| 华北 | 北京 | 北京 | 8358 | 5 | NULL |
| 华北 | 北京 | 北京 | 9773 | 8 | NULL |
| 华东 | 山东 | 青岛 | 18020133 | 8 | 12 |
| 华东 | 甘肃 | 兰州 | 7364 | NULL | 3 |
| 华东 | 湖北 | 武汉 | 7512 | NULL | 9 |
| 华东 | 江苏 | 常州 | 7582 | NULL | 5 |
| 华东 | 江苏 | 淮安 | 7670 | NULL | 7 |
+------+------+------+----------+---------+---------+
9 rows in set (0.00 sec)
mysql有没有求差集的内部函数
只是查询,不做删除吧: SELECT * FROM 表1 WHERE name NOT IN ( SELECT name FROM 表2 )
mysql里面求两个表的差集
只是查询,不做删除吧:
SELECT * FROM 表1 WHERE name NOT IN (
SELECT name FROM 表2
)
mysql数据库如何做这样的差集查询?
select URL from TABLEA
where url not in (select URL from TABLEB where APP_NAME=TABLEA.APP_NAME)
文章标题:mysql中怎么查询差集 mysql 集合差
本文网址:http://azwzsj.com/article/hpsjsc.html