资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SQL中怎么实现根据两列信息整合两张表数据-创新互联

本篇内容介绍了“SQL中怎么实现根据两列信息整合两张表数据”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联公司是一家集网站建设,太子河企业网站建设,太子河品牌网站建设,网站定制,太子河网站建设报价,网络营销,网络优化,太子河网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

两张表数据如下:

--2017年

idcollegescore
A001北京大学         670
A002中国人民大学     646
A003清华大学         664
A003清华大学         (定向)
A004北京交通大学     615
A004北京交通大学     (中外合作办学)
A005北京工业大学     
A005北京工业大学     (中外合作办学)

--2018年

idcollegescore
A001北京大学                                  680
A002中国人民大学     662
A003清华大学         671
A003清华大学         (院校特定要求)
A004北京交通大学     634
A004北京交通大学     (中外合作办学)
A005北京工业大学     
A005北京工业大学     (中外合作办学)
A006北京航空航天大学 640
A007北京理工大学     636
A007北京理工大学     (中外合作办学)
A008北京科技大学     632
Y007北京理工大学     621

需求,新表四列, id  college,s2017,s2018  两张表整合在一起,根据id、college

相关语句如下:

--创建2017/2018表
create table score2017 (id varchar2(10),college varchar2(60),s2017 int);
create table score2018 (id varchar2(10),college varchar2(60),s2018 int);
--创建集合表
create table score1718 (id varchar2(10),college varchar2(60),s2017 int,s2018 int);
--删除2017年表中重复学校和id
delete from score2017 where replace(college,' ','')='广西大学(专业志愿)';  --5行
delete from score2017 where replace(college,' ','')='河北师范大学(专业志愿)';  --2行
--插入两张表相同数据  1138行
insert into score1718 select a.id,replace(a.college,' ',''),a.s2017,b.s2018 from score2017 a,score2018 b where replace(a.college,' ','')=replace(b.college,' ','') and a.id=b.id  
select count(college) from score1718;  --1138 行重复数据
--插入2017年表中不相同数据  80行
insert into score1718 value(id,college,s2017) select a.id,replace(a.college,' ',''),a.s2017 from score2017 a where replace(a.college,' ','') not in (select replace(a.college,' ','') from score2017 a,score2018 b where replace(a.college,' ','')=replace(b.college,' ','')) 
--插入2018年表中不相同数据  134行
insert into score1718 value(id,college,s2018) select b.id,replace(b.college,' ',''),b.s2018 from score2018 b where replace(b.college,' ','') not in (select replace(a.college,' ','') from score2017 a,score2018 b where replace(a.college,' ','')=replace(b.college,' ',''))
--插入2018年表中相同学校不相同id数据   8行
insert into score1718 value(id,college,s2018) select id,college,s2018 from score2018 b where b.college in (select college from score2018 group by college having count(*) > 1) and b.id not in(select a.id from score2017 a,score2018 b where replace(a.college,' ','')=replace(b.college,' ','') and a.id=b.id)
--对比数据
select count(college) from score2017;  --1218行
select count(college) from score2018;  --1280=1138+134+8
--集合表中总数据为1360行
select count(college) from score1718;   --1360=1138+80+134+8行
--添加类型列,提取字段
alter table score1718 add (CollegeType varchar2(40));
update score1718 set CollegeType='普通' where college not like '%(%)%';  --982行
update score1718 set CollegeType=substr(college,instr(college,'(')+1,instr(college,')')-instr(college,'(')-1) where  college like '%(%)%';  --378行
select id,college,CollegeType from score1718;
--结果如下所示:
id			college					s2017 s2018  CollegeType
A650	四川外国语大学				582	   608	普通
A651	西南财经大学				619	   638	普通
A652	西南政法大学				612	   627	普通
A652	西南政法大学(中外合作办学)	599	   624	中外合作办学
A653	成都体育学院				540	   		普通
A655	四川美术学院				549	   570	普通
A656	西南民族大学				556	   582	普通
A657	贵州大学					586	   601	普通
A660	贵州医科大学					   		普通

“SQL中怎么实现根据两列信息整合两张表数据”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联-成都网站建设公司网站,小编将为大家输出更多高质量的实用文章!


当前名称:SQL中怎么实现根据两列信息整合两张表数据-创新互联
标题URL:http://cdkjz.cn/article/dcpspc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220