1. 程式人生 > 實用技巧 >MySql DQL查詢資料 (4)

MySql DQL查詢資料 (4)

4.1 DQL

(Date query language:資料庫查詢語言)

  • 所有的查詢操作都用他 query

  • 簡單的查詢,複雜的查詢都可以做

  • 資料庫最核心的語言,最重要的語言

  • 使用頻率最高的語言

Select 完整表示式:

SELECT
[ALL | DISTINCT | DISTINCTROW]
[HIGH_PRIORITY]
[STARIGHT_JOIN]
[SQL_SMALL_RESULT][SQL_BIG_RESULT][SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE][SQL_CALC_FOUND_ROWS]
SELECT_RXPR [,SELECT_RXPR...]
[FROM table_references
PARTITION partition_list]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ACS |DESC], ....[WITH ROLLUP]]
[HAVING where_condition]
[GROUP BY {col_name | expr | position}
[ACS |DESC], ....[WITH ROLLUP]]
[LIMIT [OFFSET,] ROW_COUNT | ROW_COUNT offset offset]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'fileName'
[CHARCTER SET charset_name]
export_options
| INFO DUMPFILE 'fileName'
| INFO var_name[,var_name]]
[for UPDATE | LOCK IN SHARE MODE]

4.2 指定查詢欄位

-- 查詢全部學生  select 欄位 from 表
select * from `student`;

-- 查詢指定欄位
select id,name from `student`;

-- 別名,給結果起一個別名。as 或者空格
select id as 序號,name as 名稱 from `student`;

-- 函式concat(a,b)
select concat('姓名:',name) as 新名字 from `student`;

語法 : select 欄位1,欄位2... from 表;

有的時候列明不能見名知意,我們起別名 as 欄位名 as 別名,表名 as 別名

去重 distinct

作用:去除select 中查詢出來的重複的資料,重複的資料只顯示一條

-- 查詢一下都有哪些同學參加了考試,成績
select * from result; -- 查詢結果
select student_name from result; -- 查詢參加考試的人
select distinct student_name from result; -- 查詢去除重複後參加考試的人

表示式

select version(); -- 查詢系統版本
select 100*3-1 as 結果; -- 用來計算
select @@auto_increment_increment --檢視自增的步長

-- 學員考試成績全部+1
select student_name, student_result + 1 as 成績 from result;

資料庫中的表示式:文字值,列,NULL,函式,計算表示式,系統變數......

SELECT 表示式 from 表;

4.3 where 條件子句

作用:檢索資料中符合條件的數值

搜尋得條件由一個或多個表示式組成,結果為布林值。

邏輯運算子

運算子語法描述
and && a and b a&&b 邏輯與,兩個都為真,則為真
or || a or b a||b 邏輯或,其中一個為真,則為真
not ! not a !a 邏輯非,真為假,假為真

儘量使用英文字母!

-- =============where ==================
select student_name from result;

-- 查詢成績95-100分之間
select student_name from result where student_result>95 and student_result<100;

-- and &&
select student_name from result where student_result>95 && student_result<100;

-- 模糊查詢(區間)
select student_name from result where student_result bewteen 95 and 100;

-- 除了學號1000的學生
select student_name from result where student_result != 1000;

-- != not
select student_name from result where not student_result = 1000;

模糊查詢 :比較運算子

運算子語法描述
IS NULL a is null 如果操作符為null,結果為真
IS NOT NULL a is not null 如果操作符不為null,結果為真
BETWEEN a between b and c 在b和c之間的為真
LIKE a like b sql匹配,如果a like b則為真
IN a in (b,c,d...) 假設a在b,c,d之間則為真,否則為假
-- ==============模糊查詢===================
-- 查詢姓劉的同學
select student_name from result where student_name like '劉%';

-- 查詢姓劉的同學,後面只有一個字的
select student_name from result where student_name like '劉_';

-- 查詢姓劉的同學,後面只有兩個字的
select student_name from result where student_name like '劉__';

-- 查詢中間有'美'的同學
select student_name from result where student_name like '%美%';

-- =================in======================
-- 查詢 1,2,3好學員
select student_name from result where student_id in (1,2,3);

-- 查詢在北京的學員
select student_name from result where address='' or address in ('北京');

-- ===============null not null=============
-- 查詢地址為空的學生
select student_name from result where address is null;

-- 查詢有出生日期的學生
select student_name from result where birthday is not null;

-- 查詢沒有出生日期的學生
select student_name from result where birthday is null;

4.4 聯表查詢

查詢原理圖:

-- =================聯表查詢  join===================
-- 查詢參加了考試的同學(學號,姓名,科目編號,成績)
select * from result;
select * from student;

/* 思路:
1.分析需求,分析都來自哪個表
2.確定要使用哪種連線方式? 7種
確定交叉點(兩個表中哪些資料是相同的)
判斷的條件:學生表中的studentNo = 成績表中的studentNo
*/

select s.studentNo,studentName,subjectNo,StudentResult
from student s inner join result r on s.studentNo = r.studentNo;

-- right join
select s.studentNo,studentName,subjectNo,StudentResult
from student s right join result r on s.studentNo = r.studentNo;

--left join
select s.studentNo,studentName,subjectNo,StudentResult
from student s left join result r on s.studentNo = r.studentNo;
操作描述
inner join 如果表中至少有一個匹配,就返回行
right join 即使左表中沒有匹配,也會從右表中返回所有的值
left join 即使右表中沒有匹配,也會從左表中返回所有的值

自連線

自己表和自己表連線,核心:一張表拆成兩張表即可。

父類:

categoryIdcategoryName
2 資訊科技
3 軟體開發
5 美術設計

子類

pidcategoryIdcategoryName
3 4 資料庫
2 8 辦公資訊
3 6 web開發
5 7 美術設計

操作:查詢父類對應的子類的關係

父類子類
資訊科技 辦公資訊
軟體開發 資料庫
軟體開發 web開發
美術設計 ps技術
-- 查詢父類資訊:把一張表看成兩張一摸一樣的表
select a.categoryName, b.categoryName from category a ,category b
where a.categoryId = b.pid;

4.5 分頁和排序

排序

-- 排序:升序ASC,降序 DESC
-- BRDER BY 通過這個欄位進行排序
-- 查詢成績結果,根據降序排序
select s.studentNo,studentName,SubjectName,studentResult
from student s inner join result r on s.studentNo = r.studentNo
where SubjectName = '資訊科技'
order by studentResult ASC;

分頁

-- 100w
-- 為什麼要分頁?
-- 緩解資料庫壓力,給人更好的體驗,瀑布流

-- 分頁,每頁只展示5條資料
-- 語法:limit 起始值,頁面大小
-- 網頁應用:當前,總的頁數,頁面的大小
-- LIMIT 0,5 1-5
-- LIMIT 1,5 2-6
-- LIMIT 6,5
select s.studentNo,studentName,SubjectName,studentResult
from student s inner join result r on s.studentNo = r.studentNo
where SubjectName = '資訊科技'
order by studentResult ASC
limit 5,5;
-- 第一頁 LIMIT 0,5 (1-1)*5
-- 第二頁 LIMIT 5,5 (2-1)*5
-- 第三頁 LIMIT 10,5 (3-1)*5
-- 第N頁 LIMIT N,5 (N-1)*5
-- 【pageSize,頁面大小】
-- 【(n-1)*pagesize:起始值】
-- 【n:當前頁】
-- 【資料總數/頁面大小=總頁數】

4.6 子查詢

where(這個值是計算出來的)

-- 1.查詢'資訊科技'所有考試結果
-- 方式一:你用連線查詢
select s.studentNo,studentName,SubjectName,studentResult
from student s inner join result r on s.studentNo = r.studentNo
where SubjectName = '資訊科技'
order by studentResult ASC;

-- 方式二:使用子查詢(由裡及外)
select s.studentNo,studentName,SubjectName,studentResult
from result r
where SubjectNo = (
select SubjectNo from Subject sub where SubjectName = '資訊科技'
)

-- 分數不小於80分的學員的學號和成績
select s.studentNo,studentName,SubjectName,studentResult
from student s inner join result r on s.studentNo = r.studentNo
where studentResult >= 80;

-- 分數不小於80分的學員的學號和成績
-- 查詢'資訊科技'
select s.studentNo,studentName,SubjectName,studentResult
from student s inner join result r on s.studentNo = r.studentNo
where studentResult >= 80 and SubjectName = (
select SubjectName from Subject sub where SubjectName = '資訊科技'
)

-- 改造
select studentNo,studentName from student where studentNo = (
select studentNo from result where studentResult >= 80 and SubjectName = (
select SubjectName from Subject sub where SubjectName = '資訊科技'
)
)

4.7 分組和過濾

-- 查詢不同課程的平均分,最高分,最低分,平均分大於80的學生
-- 核心:(根據不同的課程進行分組)
select studentName,
AVG(`studentResult`) AS 平均分,
MAX(`studentResult`) AS 最高分,
MIN(`studentResult`) AS 最低分
from student s inner join result r on s.studentNo = r.studentNo
group by r.studentNo
having 平均分>80;

4.8 select 小結