期末復(fù)習(xí)page5

數(shù)據(jù)查詢:
前面已經(jīng)創(chuàng)建了表,插入了數(shù)據(jù),現(xiàn)在可以進(jìn)行數(shù)據(jù)查詢。

SELECT * FROM STUDENT; --查詢student表所有數(shù)據(jù)。

SELECT sno,sname,ssex,sage,sdept FROM student;  --同上,可以查
所有列,也可以直接用*替代所有列名字。

SELECT sno,sname FROM student;  --查詢部分列

SELECT sname,2017-sage FROM student;  --查詢名字和出生的年份

SELECT sname,2017-sage as 出生年份 FROM student;   
--查詢名字和出生的年份,列名為出生年份。

SELECT sname,2017-sage 出生年份 FROM student;   --同上

SELECT sname,2017-sage 出生年份,'aaa' FROM student;   --同上,且追加輸出一列aaa

SELECT '名字是:' || sname FROM student;  --查詢名字,輸出帶 名字是: 前綴的。

SELECT sno FROM SC;  --查詢所有sno,但可能帶有重復(fù)的輸出

SELECT DISTINCT sno FROM SC;  --消除重復(fù)的輸出

SELECT DISTINCT cno,grade FROM SC;  --同時消除兩列重復(fù)的輸出。

where引出查詢條件:

SELECT sname,sage
from student
where sage>19;  --查詢sage大于19的數(shù)據(jù)

SELECT sname,sage
from student
where NOT sage<=19;  --效果同上


SELECT DISTINCT sno
FROM sc
where grade<60;  --查詢grade小于60的數(shù)據(jù),去重顯示

SELECT sname,sdept,sage
from student
where sage between 20 and 22;  --查詢sage在20到22的數(shù)據(jù)


SELECT sname,sdept,sage
from student
where sage NOT between 20 and 22;  --查詢sage不在20到22的數(shù)據(jù)

SELECT sname,ssex
from student
where sdept in('aaa','bbb');  --查詢sdept列包含aaa或bbb的數(shù)據(jù)。

SELECT sname,ssex
from student
where sdept NOT IN('aaa','bbb');  --查詢sdept列,既沒有aaa,也沒有bbb的數(shù)據(jù)
通配符
換碼
SELECT * 
from student
where sno='001';

SELECT * 
from student
where sno like '001';  --查詢效果同上

SELECT sname,sno,sex
from student
where  sname like '張%';  --查詢姓張的數(shù)據(jù)

SELECT sname,sno,sex
from student
where  sname not like '張%';  --查詢不姓張的數(shù)據(jù)

做個測試:
--插入數(shù)據(jù):
insert into cource values('4','DB_design',1,4);
insert into cource values('5','DBAdesign',1,3);

select cno,ccredit
from cource
where cname like 'DB_design';

select cno,ccredit
from cource
where cname like 'DB\_design' ESCAPE '\';  --對比一下查詢的數(shù)據(jù)結(jié)果。

is null 不能用 = null替代。


select sno,cno
from sc
where grade is null;  -- 查詢grade為空值的數(shù)據(jù)

select sno,cno
from sc
where grade is not null;  -- 查詢grade不為空值的數(shù)據(jù)

條件的優(yōu)先級:and優(yōu)先級高于or,可以用括號改變優(yōu)先級。


select sname
from student
where sdept ='aaa'
and sage<20;  --查詢sdept是aaa,sage小于20的數(shù)據(jù)。條件要同時滿足。


SELECT sname,ssex
from student
where sdept='aaa' or sdept='bbb';  
---等同于SELECT sname,ssex
from student
where sdept in('aaa','bbb');  

SELECT sname,sdept,sage
from student
where sage>=20 
and sage<=22;
--等同于SELECT sname,sdept,sage
from student
where sage between 20 and 22;
order by 子句
select sno,grade
from sc
where cno='1'
order by grade desc;   --按grade降續(xù)排列輸出

select sno,grade
from sc
where cno='1'
order by 2 desc;  --按第二列降續(xù)排列輸出

select sno,grade as 成績
from sc
where cno='1'
order by 成績 desc;  --按別名成績降續(xù)排列輸出

select * 
from student
order by sdept,sage desc;  --按sdept升續(xù)排列,同sdept的,按照sage降續(xù)排列。

select count(*)
from student;  --統(tǒng)計(jì)student表所有人數(shù)。

select avg(grade)
from sc
where cno='1';  --查詢平均值

select max(grade)
from sc
where cno='1';   --查詢最大值

數(shù)據(jù)統(tǒng)計(jì)分組
select cno,count(sno)
from sc
group by cno;  --輸出對應(yīng)cno值的統(tǒng)計(jì)量

select sno,count(*)
from sc
group by sno;  --查詢每個sno對應(yīng)的cno數(shù)量

選組條件:
select sno 
from sc
group by sno
having count(*)>2;  --查詢cno統(tǒng)計(jì)超過2個的sno,先進(jìn)行分組統(tǒng)計(jì),然后根據(jù)分組的結(jié)果進(jìn)行篩選

where
having,用在group by后面

分組查詢效果對比:

未分組將作用于整個查詢結(jié)果:
select max(grade)
from sc;  --輸出總的最大值

分組后,函數(shù)將作用于每個組
select sno,max(grade)
from sc
group by sno;  --輸出的是每個分組的最大值,而不是總的

image.png

例如:

select sno,avg(grade)  --如果這里添加了cno一起查詢,就是錯誤的
from sc
group by sno;

image.png

例如:

select sno,avg(grade)
from sc
group by sno
having avg(grade)>70;  --如果把a(bǔ)vg(grade)換為grade就是錯誤的。
image.png

例如:

selecr sno,avg(grade)
from sc
where grade<70  --這個位置不能用avg
group by sno;

select count(*)
from student
group by sdept;

select sum(grade)
from sc
group by sno;

select sno,count(*)
from sc
where grade >=90
group by sno
having count(*)>2;  --查詢sno和cno的數(shù)量,要求有兩個以上grade大于90的數(shù)量。順序是:查詢出grade>=90的記錄,按照sno分組,按count(*)>2分組,輸出sno,count(*)

集合查詢:

image.png
select cno 
from sc
where sno='001'
UNION
select cno 
from sc
where sno='002';  --講兩個查詢合并輸出,輸出已經(jīng)去重,滿足其中一個的即可,并集

select cno 
from sc
where sno='001'
INTERSECT
select cno 
from sc
where sno='002';  --必須同時滿足兩個條件的查詢。交集

select sno 
from sc
MINUS
select sno 
from sc
where cno='002';  --差集,查詢結(jié)果中,去掉cno=‘002’的數(shù)據(jù)
分組
dual表
select abs(999) from dual;  --返回999的絕對值

select mod(x,y) from dual;  --返回x除以y的余數(shù),若y=0,則返回x


alter session set NLS_DATE_FORMAT='YYYY-MM-DD';--設(shè)置當(dāng)前會話的日期格式為年月日。

select sysdate from dual;

select to_char(sysdate,'YYYY') from dual;

轉(zhuǎn)換函數(shù)
字符函數(shù)
空值函數(shù)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,853評論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • 人無好壞,只有強(qiáng)弱;事無對錯,只談利弊。
    壞壞蚊子閱讀 232評論 0 1
  • 非動畫 作個總結(jié)吧, 希望回答的朋友先看一看這個列表 《王牌酒?!?《樹影斑駁之國》 《一拳超人》 《火鳳燎原》 ...
    Natsu閱讀 3,940評論 12 44

友情鏈接更多精彩內(nèi)容