共計 2809 個字符,預(yù)計需要花費 8 分鐘才能閱讀完成。
本篇內(nèi)容介紹了“SQL 中怎么實現(xiàn)根據(jù)兩列信息整合兩張表數(shù)據(jù)”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
兩張表數(shù)據(jù)如下:
–2017 年
idcollegescoreA001 北京大學(xué) 670A002 中國人民大學(xué) 646A003 清華大學(xué) 664A003 清華大學(xué) (定向)
A004 北京交通大學(xué) 615A004 北京交通大學(xué) (中外合作辦學(xué))
A005 北京工業(yè)大學(xué)
A005 北京工業(yè)大學(xué) (中外合作辦學(xué))
–2018 年
idcollegescoreA001 北京大學(xué) 680A002 中國人民大學(xué) 662A003 清華大學(xué) 671A003 清華大學(xué) (院校特定要求)
A004 北京交通大學(xué) 634A004 北京交通大學(xué) (中外合作辦學(xué))
A005 北京工業(yè)大學(xué)
A005 北京工業(yè)大學(xué) (中外合作辦學(xué))
A006 北京航空航天大學(xué) 640A007 北京理工大學(xué) 636A007 北京理工大學(xué) (中外合作辦學(xué))
A008 北京科技大學(xué) 632Y007 北京理工大學(xué) 621
需求,新表四列,id college,s2017,s2018 兩張表整合在一起,根據(jù) id、college
相關(guān)語句如下:
-- 創(chuàng)建 2017/2018 表
create table score2017 (id varchar2(10),college varchar2(60),s2017 int);
create table score2018 (id varchar2(10),college varchar2(60),s2018 int);
-- 創(chuàng)建集合表
create table score1718 (id varchar2(10),college varchar2(60),s2017 int,s2018 int);
-- 刪除 2017 年表中重復(fù)學(xué)校和 id
delete from score2017 where replace(college, ,)= 廣西大學(xué) (專業(yè)志愿) -- 5 行
delete from score2017 where replace(college, ,)= 河北師范大學(xué) (專業(yè)志愿) -- 2 行
-- 插入兩張表相同數(shù)據(jù) 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 行重復(fù)數(shù)據(jù)
-- 插入 2017 年表中不相同數(shù)據(jù) 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 年表中不相同數(shù)據(jù) 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 年表中相同學(xué)校不相同 id 數(shù)據(jù) 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)
-- 對比數(shù)據(jù)
select count(college) from score2017; --1218 行
select count(college) from score2018; --1280=1138+134+8
-- 集合表中總數(shù)據(jù)為 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;
-- 結(jié)果如下所示:id college s2017 s2018 CollegeType
A650 四川外國語大學(xué) 582 608 普通
A651 西南財經(jīng)大學(xué) 619 638 普通
A652 西南政法大學(xué) 612 627 普通
A652 西南政法大學(xué) (中外合作辦學(xué)) 599 624 中外合作辦學(xué)
A653 成都體育學(xué)院 540 普通
A655 四川美術(shù)學(xué)院 549 570 普通
A656 西南民族大學(xué) 556 582 普通
A657 貴州大學(xué) 586 601 普通
A660 貴州醫(yī)科大學(xué) 普通
“SQL 中怎么實現(xiàn)根據(jù)兩列信息整合兩張表數(shù)據(jù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實用文章!