日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

sql查詢給表起別名要點(diǎn)小結(jié)(涉及嵌套查詢)_oracle

作者:foas9we ? 更新時(shí)間: 2022-10-31 編程語(yǔ)言

可以通過(guò)空格或者as給表起別名

但是注意如果操作的數(shù)據(jù)庫(kù)是Oracle的話,只能使用空格,as不符合Oracle的語(yǔ)法。

舉個(gè)栗子

簡(jiǎn)單查詢中使用別名

select *
from student s
where s.id = '10';

在簡(jiǎn)單的查詢中使用別名,一般沒(méi)有特別需要注意的地方,要做的操作少

復(fù)雜查詢中使用別名

題目概要:有三個(gè)表格,student(sno,sname,ssex,sbirthday,class)

score(sno,cno,degree)

course(cno,cname,tno)

查詢選修“3-105”課程的成績(jī)高于“109”號(hào)同學(xué)成績(jī)的所有同學(xué)的記錄。

答案:

select *
 from (select s.sno,s.sname,s.ssex,s.sbirthday,s.class,    sc.degree,c.cno,c.cname,c.tno from student s , course c ,score sc where s.sno = sc.sno and c.cno = sc.cno) ss
 where  ss.cno = '3-105' and ss.degree >( select degree from score where sno = '109' and cno = '3-105');

可以看到,為了方便操作,我們重新定義了一個(gè)表格ss,這個(gè)表格是一個(gè)大表格同時(shí)包含了,以上三個(gè)表中的內(nèi)容。但是要注意以下幾點(diǎn),不然容易出錯(cuò)

要全部顯示新定義表格的值時(shí),不能直接使用*

比如聲明的答案中如果改為

select *
 from (select * from student s , course c ,score sc where s.sno = sc.sno and c.cno = sc.cno) ss
 where  ss.cno = '3-105' and ss.degree >( select degree from score where sno = '109' and cno = '3-105');

命令行會(huì)顯示列未明確定義,因?yàn)槲覀円F(xiàn)在指定的列作為一個(gè)新的表,但是有些列的列名是重復(fù)的,我們需要指定其中的一個(gè)。

在嵌套查詢語(yǔ)句中無(wú)法使用新創(chuàng)的表,因?yàn)榍短撞樵兝锩娴拇a是一個(gè)完整的執(zhí)行段,會(huì)從頭開(kāi)始運(yùn)行?反正在里面調(diào)用會(huì)報(bào)錯(cuò)

select *
 from (select * from student s , course c ,score sc where s.sno = sc.sno and c.cno = sc.cno) ss
 where  ss.cno = '3-105' and ss.degree >( select degree from ss where sno = '109' and cno = '3-105');

這段SQL里面在where里面的子查詢使用了ss新表,編譯會(huì)顯示表或視圖不存在錯(cuò)誤。

總結(jié)

原文鏈接:https://blog.csdn.net/weixin_39386430/article/details/100630440

欄目分類
最近更新