第7節-MySQL資料查詢
阿新 • • 發佈:2022-12-04
1、指定列查詢
1.1、查詢所有記錄
select * from student;
1.2、查詢學號、學生名字
select sno,sname from student;
1.3、定義列的別名
select sno as 學號,sname as 學生名字 from student;
+--------+--------------+
| 學號 | 學生名字 |
+--------+--------------+
| 95001 | 洛燕妮 |
| 95002 | 歐陽炎 |
1.4、查詢並去重處理
select distinct sno from student;
1.5、查詢第5,6條記錄
select sno from student limit 4,2;
1.6、查詢前10條記錄
select sno from student limit 10;
1.7、排序查詢
-- asc 升序 -- desc 降序 select * from student order by sno desc;
2、條件查詢
2.1、查詢操作符
2.2、常見查詢方法
2.2.1、=
-- = select * from student where sno='95001';
2.2.2、>= <=
-- >= <= select *from student where age>=20 and age<=25;
2.2.3、is not null
-- is not null select * from student where age is not null;
2.2.4、between and
-- between and select * from student where age between 18 and 21;
2.2.5、in
-- in select * from student where sno in('95002','95003');
2.2.6、like
-- like select* from student where sname like '歐%';
3、regexp運算子
3.1、 regepx常用的查詢
-- regexp 以什麼結尾 select * from student where sname regexp '升$'; -- regepx 以95、94、93開頭 select * from student where sno regexp '^9[543]';
4、統計查詢
4.1、時間的運算
-- 增加生日的欄位 alter table student add birthday datetime; -- 插入一條生日的資料 insert into student values('95007','test','男','20','計算機系','2002-01-02 12:30:12'); -- now函式 select now(); +---------------------+ | now() | +---------------------+ | 2022-11-10 22:54:16 | +---------------------+ -- year、month、day、hour、minute、second函式 select year(birthday) from student; -- 計算年齡 select year(now())-year(birthday) from student;
5、聚合函式
MySQL中提供5個聚合函式 avg -- 平均值 max、min -- 最大值、最小值 sum -- 求和 count -- 計數
5.1、avg
-- 平均值 select avg(age) from student;
5.2、max、min
-- 最大值、最小值 select max(age),min(age) from student;
5.3、sum
-- 求和 select sum(age) from student;
5.4、count
-- 計數 select count(*) from student;
6、分組統計
6.1、group by
-- group by 按性別分組 select ssex,count(*) from student group by ssex;
6.2、having
-- having 按部分分組並且把結果大於4取出 select sdept,count(*) from student group by sdept having count(*)>4;
6.3、with rollup
-- with rollup 合計 select sdept,count(*) from student group by sdept with rollup having count(*)>0;
6.4、group_concat
6.4.1、去重,分組統計,列出具體的數量
-- group_concat,去重,分組統計,列出具體的數量 select sdept,count(distinct age) as 去重後部門年齡數,group_concat(distinct age) as 列出去重年齡具體資料 from student group by sdept; +----------------------------------+--------------------------+--------------------------------+ | sdept | 去重後部門年齡數 | 列出去重年齡具體資料 | +----------------------------------+--------------------------+--------------------------------+ | 020-電子資訊工程系 | 3 | 18,19,21 | | 學院-020-電子資訊工程系 | 1 | 25 | | 計算機系 | 1 | 20 | | 軟體技術 | 1 | 29 | +----------------------------------+--------------------------+--------------------------------+
6.4.2、分組統計,列出具體的數量
-- group_concat,分組統計,列出具體的數量 select sdept,count(age) as 去重後部門年齡數,group_concat(age) as 列出去重年齡具體資料 from student group by sdept; +----------------------------------+--------------------------+--------------------------------+ | sdept | 部門年齡數 | 列出年齡具體資料 | +----------------------------------+--------------------------+--------------------------------+ | 020-電子資訊工程系 | 4 | 19,21,18,21 | | 學院-020-電子資訊工程系 | 1 | 25 | | 計算機系 | 1 | 20 | | 軟體技術 | 1 | 29 | +----------------------------------+--------------------------+--------------------------------+
7、多表查詢
連線查詢分類:inner join[內連線]、outer join【right join、left join、full join】[聯合查詢]、cross join[外連線]
7.1、測試表
-- 班級表 create table student( sno varchar(10), name varchar(20), departid int ); insert into student values('1000','張三',1),('1001','李四',2),('1002','王五',3),('1003','老王',4); -- 部門表 create table department( departid int primary key, departedname varchar(20) ); insert into department values(1,'學生會'),(2,'計算機協會');
7.2、內連線【inner join】預設join
兩張表在進行連線時,連線列欄位的名稱可以不同,但要求必須具有相同資料型別,長度和精度,且表達同一範疇的意義,通常連線列欄位一般是資料表的主鍵和外來鍵。
7.2.1、inner join 內連線
select s.sno,s.name,d.departedname from student as s inner join department as d on s.departid=d.departid;
7.2.2、inner join 內連線 + where
select s.sno,s.name,d.departedname from student as s inner join department as d on s.departid=d.departid where s.name='張三';
7.2.3、inner join + using
-- 當連線條件是由兩張表相同名稱且型別也相同的欄位相連時,可以使用USIN select s.sno,s.name,d.departedname from student as s inner join department as d using(departid);
7.3、外連線【outer join】
7.3.1、左外連線[left join]
-- left join 右邊不匹配,則為null select * from student as s left join department as d on s.departid=d.departid; +------+--------+----------+----------+-----------------+ | sno | name | departid | departid | departedname | +------+--------+----------+----------+-----------------+ | 1000 | 張三 | 1 | 1 | 學生會 | | 1001 | 李四 | 2 | 2 | 計算機協會 | | 1002 | 王五 | 3 | NULL | NULL | | 1003 | 老王 | 4 | NULL | NULL | +------+--------+----------+----------+-----------------+
7.3.2、右外連線[right join]
-- right join ,左邊不匹配,則null select * from department as d right join student as s on s.departid=d.departid; +----------+-----------------+------+--------+----------+ | departid | departedname | sno | name | departid | +----------+-----------------+------+--------+----------+ | 1 | 學生會 | 1000 | 張三 | 1 | | 2 | 計算機協會 | 1001 | 李四 | 2 | | NULL | NULL | 1002 | 王五 | 3 | | NULL | NULL | 1003 | 老王 | 4 | +----------+-----------------+------+--------+----------+
7.4、完連線[full join]
-- full join ,交叉連結,兩邊資料相乘 select * from student full join department; +------+--------+----------+----------+-----------------+ | sno | name | departid | departid | departedname | +------+--------+----------+----------+-----------------+ | 1000 | 張三 | 1 | 1 | 學生會 | | 1000 | 張三 | 1 | 2 | 計算機協會 | | 1001 | 李四 | 2 | 1 | 學生會 | | 1001 | 李四 | 2 | 2 | 計算機協會 | | 1002 | 王五 | 3 | 1 | 學生會 | | 1002 | 王五 | 3 | 2 | 計算機協會 | | 1003 | 老王 | 4 | 1 | 學生會 | | 1003 | 老王 | 4 | 2 | 計算機協會 | +------+--------+----------+----------+-----------------+
7.5、聯合查詢 union 或 union all
7.5.1、準備測試資料
-- 聯合查詢的注意事項: -- 1﹑兩個查詢的列數目必須相同 -- 2﹑並且對應列的資料型別相互相容 -- 增加老師表,用於union,演示 create table teacher( sno varchar(10), name varchar(20), departid int ); insert into student values('8000','張老師',1),('8001','李老師',2);
7.5.2、union
-- 按欄位聯合查詢 union distinct 會去重顯示 select sno,name,departid from student union select sno,name,departid from teacher;
7.5.3、union all
-- 按所有欄位聯合查詢,不用去重 select * from student union all select * from teacher;
8、子查詢
8.1、集合查詢
8.1.1、in
-- in 包含 select * from student where age in (21,19);
8.1.2、not in
-- not in 不包含 select * from student where age not in (21,19);
8.1.3、any|some
-- any或some 年齡值大於21或19,則查詢出來 select * from student where age>any(select distinct age from student where age in (21,19));
8.1.4、all
-- all 年齡的值大於21且19,則查詢出來 elect * from student where age>all(select distinct age from student where age in (21,19));
8.1.5、exists
-- exists 查詢學生表在課程表有記錄的學生 select * from student where exists(select * from score where student.sno=score.sno);
8.2、子查詢-增
-- 複製表結構 create table stu_exists like student; -- 查詢結果,並且寫入表格中 insert into stu_exists select * from student where sno in(select distinct sno from score);
8.3、子查詢-刪
-- 刪除學生表資訊 delete from student where sno in (select distinct sno from score);
8.4、子查詢-改
-- 修改成績表 update score set grade=85 where sno=(select sno from student where sno='95001') and courseid=1;
8.5、子查詢-查
-- 查詢有成績的學生資訊 select * from student where sno in(select distinct sno from score);