MySQL學習 二 使用DQL查詢資料

2021-09-25 05:35:01 字數 2305 閱讀 6774

dql(data query language 資料查詢語言)可用來查詢資料庫資料,進行簡單的單錶查詢或者多表的複雜查詢和巢狀查詢。

# 查詢表中所有的資料列結果,採用萬用字元*

# 示例1:查詢student表中的所有資訊

select * from student;

# 示例2:查詢student表中的指定列(學號,姓名)

select studentno,studentname from student;

# as關鍵字可以給資料列取乙個新的別名,在聯合查詢中方便區別不同表的相同的列名

# 示例:查詢student表中的指定列(學號,姓名),並將列名顯示為學號,姓名

select studentno as '學號',studentname as '姓名' from student;

# 使用distinct可以將查詢返回的記錄結果中的重覆記錄去掉,即當返回的某些列的值相同時,只返回一條記錄

# 示例:查詢studenno並去除重複學號的ji

select distinct studentno from student;

# where用於檢索資料表中符合條件的記錄

# 搜尋條件可由乙個或多個邏輯表示式組成 , 結果一般為真或假

# 邏輯操作符 and(可寫為&&) or(可寫為||) not(可寫為!)

# 示例

# 查詢考試成績在95-100之間的

select studentno,studentresult

from result

where studentresult>=95 and studentresult<=100;

# 查詢學號為1009的同學以外的其他同學的成績

select studentno,studentresult

from result

where not studentno=1009;

#查詢科目8成績在90分到100分之間的同學的學號

select studentno

from result

where subjectno=8 and studentresult between 90 and 100;

# 需要結合萬用字元使用

# 查詢所欲李姓同學的學號及姓名

select studentno,studentname from student

where studentname like '李%';

# 查詢李姓且名字只有兩個字的同學的學號及姓名

select studentno,studentname from student

where studentname like '李_';

# 查詢李姓且名字只有三個字的同學的學號及姓名

select studentno,studentname from student

where studentname like '李_';

# 查詢姓名中含有文字的同學的學號及姓名(文字出現的位置不限)

select studentno,studentname from student

where studentname like '%文%';

# 查詢address為北京,南京,河南洛陽的學生

select studentno,studentname,address from student

where address in ('北京','南京','河南洛陽');

#查詢科目8成績在90分到100分之間的同學的姓名(使用巢狀連線查詢)

select studentname

from student

where studentno

in(select studentno from result where subjectno=8 and studentresult between 90 and 100);

#查詢出生日期沒有填寫的同學(注意,不能用=null判斷,要使用is null判斷)

select studentname from student

where borndate is null;

#查詢出生日期填寫了的同學

select studentname from student

where borndate is not null;

# 需要注意的是,空字串''並不等於null

使用DQL查詢資料

回顧dml insert into student stuname,stupwd,gender,gradeid,address values 張三 123 男 1,北京西城 王五 123 女 2,北京西城 田七 123 男 3,北京宣武 dql data query language 資料查詢語言 ...

MySQL查詢(二) 排序查詢(DQL語言)

select 查詢列表 from 表 where 篩選條件 order by 排序列表 asc desc 1 按單個字段排序 select from employees order by salary desc 2 新增篩選條件再排序 案例 查詢部門編號 90的員工資訊,並按員工編號降序 selec...

MySQL的DQL排序查詢

mysql列表頁 語法 select 查詢列表 from 表名 where 篩選條件 order by 排序的字段或表示式 特點 1 asc代表的是公升序,可以省略 desc代表的是降序 2 order by子句可以支援 單個字段 別名 表示式 函式 多個字段 3 order by子句在查詢語句的最...