本篇內(nèi)容介紹了“SQL中怎么實(shí)現(xiàn)根據(jù)兩列信息整合兩張表數(shù)據(jù)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),太子河企業(yè)網(wǎng)站建設(shè),太子河品牌網(wǎng)站建設(shè),網(wǎng)站定制,太子河網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,太子河網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。兩張表數(shù)據(jù)如下:
--2017年
id | college | score |
A001 | 北京大學(xué) | 670 |
A002 | 中國(guó)人民大學(xué) | 646 |
A003 | 清華大學(xué) | 664 |
A003 | 清華大學(xué) (定向) | |
A004 | 北京交通大學(xué) | 615 |
A004 | 北京交通大學(xué) (中外合作辦學(xué)) | |
A005 | 北京工業(yè)大學(xué) | |
A005 | 北京工業(yè)大學(xué) (中外合作辦學(xué)) |
--2018年
id | college | score |
A001 | 北京大學(xué) | 680 |
A002 | 中國(guó)人民大學(xué) | 662 |
A003 | 清華大學(xué) | 671 |
A003 | 清華大學(xué) (院校特定要求) | |
A004 | 北京交通大學(xué) | 634 |
A004 | 北京交通大學(xué) (中外合作辦學(xué)) | |
A005 | 北京工業(yè)大學(xué) | |
A005 | 北京工業(yè)大學(xué) (中外合作辦學(xué)) | |
A006 | 北京航空航天大學(xué) | 640 |
A007 | 北京理工大學(xué) | 636 |
A007 | 北京理工大學(xué) (中外合作辦學(xué)) | |
A008 | 北京科技大學(xué) | 632 |
Y007 | 北京理工大學(xué) | 621 |
需求,新表四列, id college,s2017,s2018 兩張表整合在一起,根據(jù)id、college
相關(guān)語(yǔ)句如下:
--創(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) --對(duì)比數(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 四川外國(guó)語(yǔ)大學(xué) 582 608 普通 A651 西南財(cái)經(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中怎么實(shí)現(xiàn)根據(jù)兩列信息整合兩張表數(shù)據(jù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
分享文章:SQL中怎么實(shí)現(xiàn)根據(jù)兩列信息整合兩張表數(shù)據(jù)-創(chuàng)新互聯(lián)
文章分享:http://www.rwnh.cn/article32/dcpspc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、品牌網(wǎng)站制作、網(wǎng)站維護(hù)、網(wǎng)站制作、網(wǎng)站營(yíng)銷、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容