旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

用SQL举行鸠合运算【MySQL教程】,运算,集合,进行

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:28评论:0


导读:这篇文章重要引见了关于用SQL举行鸠合运算,有着肯定的参考价值,如今分享给人人,有须要的朋侪能够参考一下1、比较表和表droptableifexiststbl_a...

这篇文章重要引见了关于用SQL举行鸠合运算 ,有着肯定的参考价值,如今分享给人人,有须要的朋侪能够参考一下

1、比较表和表

drop table if exists tbl_a;create table tbl_a(
key1 varchar(10),
col_1 int4,
col_2 int4,
col_3 int4
);insert into tbl_a values('A', 2, 3, 4);
insert into tbl_a values('B', 0, 7, 9);
insert into tbl_a values('c', 5, 1, 6);
drop table if exists tbl_b;create table tbl_b(
key1 varchar(10),
col_1 int4,
col_2 int4,
col_3 int4
);
insert into tbl_b values('A', 2, 3, 4);
insert into tbl_b values('B', 0, 7, 9);
insert into tbl_b values('c', 5, 1, 6);-- ## 假如union a b 行数一致则两张表相称 select count(1) row_cnt  from ( select * 
           from tbl_A           union 
          select *            from tbl_b
        ) tmp
;


直接求两表的不同之处

(select * from tbl_a except
 select * from tbl_b) union all
 (select * from tbl_b  except
  select * from tbl_a);

2、用差集完成关联除法运算

建表

drop table if exists skills;create table skills(
skill varchar(10)
);insert into skills values('oracle');
insert into skills values('unix');insert into skills values('java');drop table if exists empskills;create table empskills(
emp varchar(10),
skill varchar(10)
);insert into empskills values('相田','oracle');
insert into empskills values('相田','unix');
insert into empskills values('相田','java');
insert into empskills values('相田','c#');
insert into empskills values('奇异','oracle');
insert into empskills values('奇异','unix');
insert into empskills values('奇异','java');
insert into empskills values('平井','oracle');
insert into empskills values('平井','unix');
insert into empskills values('平井','PHP');
insert into empskills values('平井','Perl');
insert into empskills values('平井','C++');
insert into empskills values('若田部','Perl');
insert into empskills values('度来','oracle');
--把除法变成减法select distinct emp  from empskills es1 where not exists
        (select skill from skills
         expect         select skill from empskills es2          where es1.emp = es2.emp);

3、追求相称的子集

drop table if exists supparts;create table supparts(
sup varchar(10),
part varchar(10)
);insert into supparts values('A', '螺丝');
insert into supparts values('A', '螺母');
insert into supparts values('A', '管子');
insert into supparts values('B', '螺丝');
insert into supparts values('B', '管子');
insert into supparts values('C', '螺丝');
insert into supparts values('C', '螺母');
insert into supparts values('C', '管子');
insert into supparts values('D', '螺丝');
insert into supparts values('D', '管子');
insert into supparts values('E','保险丝');
insert into supparts values('E', '螺母');
insert into supparts values('E', '管子');
insert into supparts values('F','保险丝');

思绪: 两个供给商都运营同种范例的零件 (简朴的根据零件列举行衔接) 两个供给商的零件范例数雷同(即存在逐一映照)(count限制)

select a.sup s1, b.sup s2  from supparts a, supparts b where a.sup < b.sup                       -- 生成供给商的悉数组合 
   and a.part = b.part                     -- 前提1:运营同种范例的零件 
 group by a.sup, b.suphaving count(*) = (select count(1)         -- 前提2:运营的零件的数目品种雷同 a = 中间数                     from supparts c                    where c.sup = a.sup)   and count(*) = (select count(1)         -- 前提2:运营的零件的数目品种雷同 b = 中间数                     from supparts d                    where d.sup = b.sup)
;

4、删除重行

drop table if exists products;create table products(
rowid int4,
name1 varchar(10),
price int4
);insert into products values(1,'苹果',50);insert into products values(2,'橘子',100);
insert into products values(3,'橘子',100);insert into products values(4,'橘子',100);
insert into products values(5,'香蕉',80);-- 删除重行高效SQL语句(1):经由过程EXCEPT求补集delete from productswhere rowid  in (select rowid           -- 悉数rowid                   from products 
                 except                 -- 减去                 select max(rowid)      -- 要留下的rowid                   from products                  group by name1, price
                  );-- 删除重行高效SQL语句(2):经由过程not indelete from products where rowid not in (select max(rowid)                      from products                     group by name1, price
                    );

演习

-- 革新顶用union的比较select 
    case when count(1) = (select count(1) from tbl_A)          
    and count(1) = (select count(1)+1 from tbl_b)         
    then count(1) else '不相称' end row_cnt  from ( select * from tbl_A          union 
         select * from tbl_b
        ) tmp
;

内容多来自 《SQL进阶课本》,仅做笔记。演习部份代码均为原创。

以上就是用SQL举行鸠合运算 的细致内容,更多请关注ki4网别的相干文章!

标签:运算集合进行


欢迎 发表评论: