網(wǎng)站首頁(yè) 編程語(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
相關(guān)推薦
- 2022-10-09 .NET擴(kuò)展方法使用實(shí)例詳解_實(shí)用技巧
- 2022-12-04 Android?MQTT與WebSocket協(xié)議詳細(xì)講解_Android
- 2022-11-24 詳解python中@classmethod和@staticmethod方法_python
- 2022-04-28 python實(shí)現(xiàn)簡(jiǎn)單的學(xué)生成績(jī)管理系統(tǒng)_python
- 2022-04-03 深入了解Python如何操作MongoDB_python
- 2022-06-14 C語(yǔ)言選擇、循環(huán)、函數(shù)、數(shù)組與操作符_C 語(yǔ)言
- 2023-01-14 Go?庫(kù)bytes.Buffer和strings.Builder使用及性能對(duì)比_Golang
- 2022-10-11 Systemtap BPF/BCC bpftrace 實(shí)踐對(duì)比
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支