SQL中怎么实现根据两列信息整合两张表数据
发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,本篇内容介绍了"SQL中怎么实现根据两列信息整合两张表数据"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所
千家信息网最后更新 2025年11月15日SQL中怎么实现根据两列信息整合两张表数据
本篇内容介绍了"SQL中怎么实现根据两列信息整合两张表数据"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
两张表数据如下:
--2017年
| id | college | score |
| A001 | 北京大学 | 670 |
| A002 | 中国人民大学 | 646 |
| A003 | 清华大学 | 664 |
| A003 | 清华大学 (定向) | |
| A004 | 北京交通大学 | 615 |
| A004 | 北京交通大学 (中外合作办学) | |
| A005 | 北京工业大学 | |
| A005 | 北京工业大学 (中外合作办学) |
--2018年
| id | college | score |
| 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年表中重复学校和iddelete 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 CollegeTypeA650 四川外国语大学 582 608 普通A651 西南财经大学 619 638 普通A652 西南政法大学 612 627 普通A652 西南政法大学(中外合作办学) 599 624 中外合作办学A653 成都体育学院 540 普通A655 四川美术学院 549 570 普通A656 西南民族大学 556 582 普通A657 贵州大学 586 601 普通A660 贵州医科大学 普通"SQL中怎么实现根据两列信息整合两张表数据"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
大学
北京
数据
普通
合作办学
办学
合作
相同
交通
交通大学
工业
年表
北京工业大学
清华
清华大学
整合
理工
北京理工大学
信息
专业
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
aja钻石数据库
刺激战场美服的服务器
数据库两级映射功能
服务器被局域网攻击有用吗
广东个性化软件开发厂家报价
xx镇反网络安全宣讲主持词
分布式架构数据库
郑州中关村互联网科技集体
同一个服务器做备份
mc自己开服务器
江西青少年网络安全题库
基岩版怎么进入小游戏服务器
呼吁网络安全的话
数据库可以申请知识产权吗
学校网络安全工作会议讲话
东方航空招聘软件开发
彻底删除数据库语句
公安机关网络安全检查工作部署会
mr12u打印服务器
数据库只保存5分钟数据
机房服务器如何搬迁
网络技术大一知识点
centos日志服务器
方舟pve服务器介绍
软件开发大赛如何评分细则
网络安全海报儿童画
2012网络安全年会
幼儿园网络安全与保密协议
医疗卫生网络安全解决方案
济南房地产交易数据库