資料庫-DML增刪改查
阿新 • • 發佈:2021-01-17
1、MYSQL的插入操作
insert語句
語法:
INSERT INTO table [(column [, column...])] –VALUES(value [, value...]),(value [, value...])
eg: 1、建立表emp mysql> create table emp( -> id int primary key auto_increment, -> name char(16) not null, -> gender enum('男','女') default '男', -> address char(16)); 2、一次插入一條記錄(預設) mysql> insert into emp(id,name) values(20210001,'jim'); Query OK, 1 row affected (0.01 sec) mysql> insert into emp(name) values('susan'); Query OK, 1 row affected (0.00 sec) mysql> insert into emp values(20210003,'joan','女','陝西西安'); Query OK, 1 row affected (0.00 sec) mysql> select * from emp; +----------+-------+--------+--------------+ | id | name | gender | address | +----------+-------+--------+--------------+ | 20210001 | jim | 男 | NULL | | 20210002 | susan | 男 | NULL | | 20210003 | joan | 女 | 陝西西安 | +----------+-------+--------+--------------+ 3、一次插入多條記錄 mysql> insert into emp(name) values('john'),('smith'),('jane'); 4、建立表username mysql> create table username(name char(16)); 5、將查詢出的資訊插入其他表 mysql> insert into username -> select name from emp; mysql> select * from username; +-------+ | name | +-------+ | jim | | susan | | joan | | john | | smith | | jane | +-------+ 如果需要插入其他特殊字元,應該採用\轉義字元做字首
replace語句
語法1:replace into 表名 [(欄位列表)] values (值列表)
語法2:replace [into] 目標表名[(欄位列表1)] select (欄位列表2) from 源表 where 條件表示式
語法3:replace [into] 表名 set 欄位1=值1, 欄位2=值2
eg: mysql> replace into username values('w5'); mysql> replace into username -> select name from emp mysql> replace into username -> set name='l4';
replace 與 insert區別
replace語句向表插入新記錄時,若新記錄的主鍵值或者唯一性約束的欄位與已有記錄相同,則已有記錄先被刪除(已有記錄刪除時不能違背外來鍵約束條件),然後再插入新記錄。
replace可以將delect與insert合二為一,形成原子操作,無需將delect與insert操作置於事務之中
eg: mysql> alter table username modify name char(16) unique; 將name欄位修改為唯一值,表中不能重複 mysql> select * from username; +-------+ | name | +-------+ | jane | | joan | | john | | smith | | susan | | z6 | +-------+ 用insert插入報錯 mysql> insert into username values('z6'); ERROR 1062 (23000): Duplicate entry 'z6' for key 'name' 用replace插入不報錯,先刪除再插入 mysql> replace into username values('z6'); Query OK, 1 row affected (0.00 sec)
2、MYSQL的更新操作
update語句
語法:–UPDATE table SET column = value [, column = value] … [WHERE condition];
eg:
mysql> update emp
-> set gender='女'
-> where name='susan';
mysql> update emp
-> set gender='女',address='山東青島'
-> where name like 'j%';
mysql> update emp
-> set address='遼寧大連'
-> where name regexp '^jo';
3、MYSQL的刪除操作
truncat 語句(保留表結構,清空表中所有資料)
語法:TRUNCATE TABLE table_name
完全清空一個表
DDL語句
eg:
mysql> truncate table username;
delete語句 (可以與where進行條件刪除)
語法:TRUNCATE TABLE table_name where ...
eg:
mysql> delete from username
-> where name like 's%';
drop 語句(刪除資料與表結構)
語法:DROP TABLE table_name;
eg:
mysql> drop table username;
DROP、TRUNCATE、DELETE對比
delete**刪除資料,保留表結構,可以回滾,如果資料量大,很慢
truncate** 刪除所有資料,保留表結構,不可以回滾,一次全部刪除所有資料,速度相對很快
drop** 刪除資料和表結構,刪除速度最快。
4、MYSQL的查詢操作
select語句
語法:SELECT {*, column [alias],...} FROM table;
eg:
mysql> select * from emp;
mysql> select id,name from emp;
mysql> select id as 編號,name 姓名 from emp; AS 作用:起別名
+----------+--------+
| 編號 | 姓名 |
+----------+--------+
| 20210006 | jane |
| 20210003 | joan |
+----------+--------+
select語句中的算數表示式
對數值型資料列、變數、常量可以使用算數操作符建立表示式(+ -* /)
對日期型資料列、變數、常量可以使用部分算數操作符建立表示式(+ -)
-- MySQL的+預設只有一個功能:運算子
SELECT 100+80; # 結果為180
SELECT '123'+80; # 只要其中一個為數值,則試圖將字元型轉換成數值,轉換成功做預算,結果為203
SELECT 'abc'+80; # 轉換不成功,則字元型數值為0,結果為80
SELECT 'This'+'is'; # 轉換不成功,結果為0
SELECT NULL+80; # 只要其中一個為NULL,則結果為NULL
eg:
mysql> select 10+2*3/4;
+----------+
| 10+2*3/4 |
+----------+
| 11.5000 |
+----------+
mysql> alter table emp add salary float(6,2) after gender; #在gender列後插入salary列
mysql> update emp
-> set salary=8500.0;
mysql> select salary*14 年薪 from emp;
+-----------+
| 年薪 |
+-----------+
| 119000.00 |
| 119000.00 |
| 119000.00 |
| 119000.00 |
| 119000.00 |
| 119000.00 |
+-----------+
mysql> update emp
-> set salary=salary*1.5
-> where gender='男';
mysql> select 11+12;
+-------+
| 11+12 |
+-------+
| 23 |
+-------+
mysql> select '23'+22;
+---------+
| '23'+22 |
+---------+
| 45 |
+---------+
mysql> select 'a'+180;
+---------+
| 'a'+180 |
+---------+
| 180 |
+---------+
mysql> select 'sw'+'K';
+----------+
| 'sw'+'K' |
+----------+
| 0 |
+----------+
mysql> select null+12;
+---------+
| null+12 |
+---------+
| NULL |
+---------+
select 運算子的優先順序
乘法和除法的優先順序高於加法和減法
同級運算的順序是從左到右
表示式中使用括號可強行改變優先順序的運算順序
NULL值的使用
空值是指不可用、未分配的值
空值不等於零或空格
任意型別都可以支援空值
包括空值的任何算術表示式都等於空
字串和null進行連線運算,得到也是null.
安全等於<=>
1.可作為普通運算子的=
2.也可以用於判斷是否是NULL
eg:
mysql> select * from emp
-> where salary<=>NUll;
+----------+------+--------+--------+---------+
| id | name | gender | salary | address |
+----------+------+--------+--------+---------+
| 20210007 | xyz | 男 | NULL | NULL |
+----------+------+--------+--------+---------+
1 row in set (0.00 sec)
定義欄位的別名
改變列的標題頭
用於表示計算結果的含義
作為列的別名
如果別名中使用特殊字元,或者是強制大小寫敏感,或有空格時,都可以通過為別名新增加雙引號實現。
mysql> select gender as 性別
-> from emp;
+--------+
| 性別 |
+--------+
| 男 |
| 女 |
| 女 |
| 女 |
| 男 |
| 女 |
| 男 |
+--------+
重複記錄
預設情況下查詢顯示所有行,包括重複行
使用DISTINCT關鍵字可從查詢結果中清除重複行
DISTINCT的作用範圍是後面所有欄位的組合
語法:
SELECT DISTINCT address
FROM emp;
mysql> select distinct address from emp;
+--------------+
| address |
+--------------+
| NULL |
| 遼寧大連 |
| 山東青島 |
+--------------+
mysql> select distinct salary,gender
-> from emp;
+---------+--------+
| salary | gender |
+---------+--------+
| 9350.00 | 男 |
| 8500.00 | 女 |
| NULL | 男 |
+---------+--------+
限制所選擇的記錄
使用WHERE子句限定返回的記錄
WHERE子句在FROM 子句後
SELECT[DISTINCT] {*, column [alias], ...}
FROM table–[WHEREcondition(s)];
WHERE中的字串和日期值
字串和日期要用單引號擴起來
字串是大小寫敏感的,日期值是格式敏感的
比較運算子
1、<,=,>
2、between .... and...
eg:
mysql> select name,gender,salary
-> from emp
-> where salary<=9000;
mysql> select name,gender,salary
-> from emp
-> where salary between 8600 and 9500;
in運算子
使用IN運算子獲得匹配列表值的記錄
mysql> select name,gender
-> from emp
-> where address in('山東青島','遼寧大連');
+------+--------+
| name | gender |
+------+--------+
| joan | 女 |
| john | 女 |
| jane | 女 |
+------+--------+
like運算子
使用LIKE運算子執行模糊查詢
查詢條件可包含文字字元或數字
(%) 可表示零或多個字元
( _ ) 可表示一個字元
eg:
mysql> select name
-> from emp
-> where name like 'j%';
+------+
| name |
+------+
| jane |
| joan |
| john |
+------+
mysql> select name from emp where name like '_a%';
+------+
| name |
+------+
| jane |
+------+
is null運算子
查詢包含空值的記錄
eg:
mysql> select name,address
-> from emp
-> where address is null;
+-------+---------+
| name | address |
+-------+---------+
| z6 | NULL |
| susan | NULL |
| smith | NULL |
| xyz | NULL |
+-------+---------+
邏輯運算子
使用AND運算子:AND需要所有條件都是滿足
使用OR運算子:OR只要兩個條件滿足一個就可以
使用NOT運算子:NOT是取反的意思
eg:
mysql> select name
-> from emp
-> where salary>9000 and address is null;
+-------+
| name |
+-------+
| z6 |
| smith |
+-------+
mysql> select name from emp where salary>9000 or address is null;
+-------+
| name |
+-------+
| z6 |
| susan |
| smith |
| xyz |
+-------+
mysql> select name from emp where not salary>9000 ;
+-------+
| name |
+-------+
| susan |
| joan |
| john |
| jane |
+-------+
對結果集排序
語法:order by 列名[desc],列名…
預設升序排序:asc
降序排序:desc
常用聚合函式
聚合函式對一組值進行運算,並返回單個值。也叫組合函式。
COUNT(*|列名) 統計行數
AVG(數值型別列名) 平均值
SUM (數值型別列名) 求和
MAX(列名) 最大值
MIN(列名) 最小值
除了COUNT()以外,聚合函式都會忽略NULL值
mysql> select max(salary),min(salary),avg(salary),sum(salary) from emp;
+-------------+-------------+-------------+-------------+
| max(salary) | min(salary) | avg(salary) | sum(salary) |
+-------------+-------------+-------------+-------------+
| 9350.00 | 6500.50 | 8457.214286 | 59200.50 |
+-------------+-------------+-------------+-------------+
數值型函式
eg:
mysql> select abs(-5);
+---------+
| abs(-5) |
+---------+
| 5 |
+---------+
mysql> select sqrt(9);
+---------+
| sqrt(9) |
+---------+
| 3 |
+---------+
mysql> select pow(2,3);
+----------+
| pow(2,3) |
+----------+
| 8 |
+----------+
mysql> select mod(5,2);
+----------+
| mod(5,2) |
+----------+
| 1 |
+----------+
mysql> select rand(1);
+---------------------+
| rand(1) |
+---------------------+
| 0.40540353712197724 |
+---------------------+
字串函式
eg:
mysql> select length('hello world.');
+------------------------+
| length('hello world.') |
+------------------------+
| 12 |
+------------------------+
mysql> select length('中國'); utf8一個漢字3個位元組,dbk一個漢字2個位元組
+------------------+
| length('中國') |
+------------------+
| 6 |
+------------------+
mysql> select concat('hello',' world');
+--------------------------+
| concat('hello',' world') |
+--------------------------+
| hello world |
+--------------------------+
mysql> select lower('Hello World');
+----------------------+
| lower('Hello World') |
+----------------------+
| hello world |
+----------------------+
mysql> select upper('hello world');
+----------------------+
| upper('hello world') |
+----------------------+
| HELLO WORLD |
+----------------------+
mysql> select left('hello',2);
+-----------------+
| left('hello',2) |
+-----------------+
| he |
+-----------------+
mysql> select right('hello',2);
+------------------+
| right('hello',2) |
+------------------+
| lo |
+------------------+
mysql> select trim(' hello ');
+------------------+
| trim(' hello ') |
+------------------+
| hello |
+------------------+
日期和時間函式
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2021-01-17 |
+------------+
1 row in set (0.00 sec)
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 10:57:00 |
+-----------+
mysql> select concat(curdate(),' ',curtime());
+---------------------------------+
| concat(curdate(),' ',curtime()) |
+---------------------------------+
| 2021-01-17 10:57:51 |
+---------------------------------+
mysql> select now(),sysdate();
+---------------------+---------------------+
| now() | sysdate() |
+---------------------+---------------------+
| 2021-01-17 10:59:14 | 2021-01-17 10:59:14 |
+---------------------+---------------------+
mysql> select day(now()),year(now()),month(now());
+------------+-------------+--------------+
| day(now()) | year(now()) | month(now()) |
+------------+-------------+--------------+
| 17 | 2021 | 1 |
+------------+-------------+--------------+
mysql> select dayname(now()),monthname(now());
+----------------+------------------+
| dayname(now()) | monthname(now()) |
+----------------+------------------+
| Sunday | January |
+----------------+------------------+
mysql> select week(now());
+-------------+
| week(now()) |
+-------------+
| 3 |
+-------------+
mysql> select dayofyear(now());
+------------------+
| dayofyear(now()) |
+------------------+
| 17 |
+------------------+
mysql> select dayofmonth(now());
+-------------------+
| dayofmonth(now()) |
+-------------------+
| 17 |
+-------------------+
流程控制函式
mysql> select if(2<3,2,3); 若1為真取2,,否則取3
+-------------+
| if(2<3,2,3) |
+-------------+
| 2 |
+-------------+
mysql> select if(2>3,2,3);
+-------------+
| if(2>3,2,3) |
+-------------+
| 3 |
+-------------+
mysql> select ifnull(1,2),ifnull(2<3,2),ifnull(2>3,4);
+-------------+---------------+---------------+
| ifnull(1,2) | ifnull(2<3,2) | ifnull(2>3,4) |
+-------------+---------------+---------------+
| 1 | 1 | 0 |
+-------------+---------------+---------------+
mysql> select case when 2<4 then 'true' else 'false' end;
+--------------------------------------------+
| case when 2<4 then 'true' else 'false' end |
+--------------------------------------------+
| true |
+--------------------------------------------+
1 row in set (0.00 sec)
mysql> select case when 1<0 then 'true' else 'false' end;
+--------------------------------------------+
| case when 1<0 then 'true' else 'false' end |
+--------------------------------------------+
| false |
+--------------------------------------------+
mysql> select name,salary,
-> case salary
-> when salary<8500 then salary*1.2
-> when salary>8500 then salary*0.6
-> else salary
-> end as 工資 from emp;
+-------+---------+---------+
| name | salary | 工資 |
+-------+---------+---------+
| z6 | 9350.00 | 9350.00 |
| susan | 8500.00 | 8500.00 |
| joan | 8500.00 | 8500.00 |
| john | 8500.00 | 8500.00 |
| smith | 9350.00 | 9350.00 |
| jane | 8500.00 | 8500.00 |
| xyz | 6500.50 | 6500.50 |
+-------+---------+---------+
資料分組(group by)
分組的含義是:把該列具有相同值的多條記錄當成一組記錄處理,最後只輸出一條記錄。
分組函式忽略空值.
分組函式重要規則:
1、如果使用了分組函式,或者使用GROUP BY 的查詢:出現在SELECT列表中的欄位,要麼出現在組合函式裡, 要麼出現在GROUP BY 子句中。
2、GROUP BY 子句的欄位可以不出現在SELECT列表當中。
3、使用集合函式可以不使用GROUP BY子句,此時所有的查詢結果作為一組。
mysql> select gender,avg(salary)
-> from emp
-> group by gender;
+--------+-------------+
| gender | avg(salary) |
+--------+-------------+
| 男 | 8400.166667 |
| 女 | 8500.000000 |
+--------+-------------+
限定組的結果(having子句)
HAVING子句用來對分組後的結果再進行條件過濾。
格式:
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BYcolumn];
having與where區別
WHERE是在分組前進行條件過濾, HAVING子句是在分組後進行條件過濾,
WHERE子句中不能使用聚合函式, HAVING子句可以使用聚合函式。
多行資料合併 GROUP_CONCAT
注意:使用 GROUP_CONCAT()函式必須對源資料進行分組,否則所有資料會被合併成一行
mysql> select s_id,group_concat(f_name) from fruits group by s_id;
+------+-------------------------+
| s_id | group_concat(f_name) |
+------+-------------------------+
| 101 | apple,blackberry,cherry |
| 102 | orange,banana,grape |
| 103 | apricot,coconut |
| 104 | berry,lemon |
| 105 | melon,xbabay,xxtt |
| 106 | mango |
| 107 | xxxx,xbababa |
+------+-------------------------
聯合查詢
UNION 去重複
UNION ALL 不去重複
mysql> select *from fruits where s_id=101
-> union
-> select *from fruits where s_id=102;
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b1 | 101 | blackberry | 10.20 |
| c0 | 101 | cherry | 3.20 |
| bs1 | 102 | orange | 11.20 |
| t1 | 102 | banana | 10.30 |
| t2 | 102 | grape | 5.30 |
+------+------+------------+---------+
mysql> select *from fruits where s_id=101 union all select *from fruits where f_name='apple';
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b1 | 101 | blackberry | 10.20 |
| c0 | 101 | cherry | 3.20 |
| a1 | 101 | apple | 5.20 |
+------+------+------------+---------+
mysql> select *from fruits where s_id=101 union select *from fruits where f_name='apple';
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b1 | 101 | blackberry | 10.20 |
| c0 | 101 | cherry | 3.20 |
+------+------+------------+---------+
查詢結果限定
在SELECT語句最後可以用LIMLT來限定查詢結果返回的起始記錄和總數量。MySQL特有
SELECT … LIMIT offset_start,row_count;
offset_start:第一個返回記錄行的偏移量。預設為0.
row_count:要返回記錄行的最大數目。
SELECT * FROM TB_EMP LIMIT 5;/*檢索前5個記錄*/
SELECT * FROM TB_EMP LIMIT 5,10;/*檢索記錄行6-15*/
mysql萬用字元
%:用來表示任意多個字元,包含0個字元
_ : 用來表示任意單個字元
escape:用來轉義特定字元
正則表示式
實現一些很特殊的規則匹配;
正則表示式需要使用REGEXP命令,匹配上返回"1"匹配不上返回"0",預設不加 條件REGEXP相當於like ‘%%’。
在前面加上NOT相當於NOT LIKE。
單表查詢練習題1
CREATE TABLE fruits
(
f_id char(10) NOT NULL,
s_id INT NOT NULL,
f_name char(255) NOT NULL,
f_price decimal(8,2) NOT NULL,
PRIMARY KEY(f_id)
);
INSERT INTO fruits (f_id, s_id, f_name, f_price)
VALUES('a1', 101,'apple',5.2),
('b1',101,'blackberry', 10.2),
('bs1',102,'orange', 11.2),
('bs2',105,'melon',8.2),
('t1',102,'banana', 10.3),
('t2',102,'grape', 5.3),
('o2',103,'coconut', 9.2),
('c0',101,'cherry', 3.2),
('a2',103, 'apricot',2.2),
('l2',104,'lemon', 6.4),
('b2',104,'berry', 7.6),
('m1',106,'mango', 15.6),
('m2',105,'xbabay', 2.6),
('t4',107,'xbababa', 3.6),
('m3',105,'xxtt', 11.6),
('b5',107,'xxxx', 3.6)
【例1】從fruits表中檢索所有欄位的資料,SQL語句如下:
mysql> select * from fruits;
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| a2 | 103 | apricot | 2.20 |
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
| b5 | 107 | xxxx | 3.60 |
| bs1 | 102 | orange | 11.20 |
| bs2 | 105 | melon | 8.20 |
| c0 | 101 | cherry | 3.20 |
| l2 | 104 | lemon | 6.40 |
| m1 | 106 | mango | 15.60 |
| m2 | 105 | xbabay | 2.60 |
| m3 | 105 | xxtt | 11.60 |
| o2 | 103 | coconut | 9.20 |
| t1 | 102 | banana | 10.30 |
| t2 | 102 | grape | 5.30 |
| t4 | 107 | xbababa | 3.60 |
+------+------+------------+---------+
【例2】查詢fruits表中f_name列所有水果名稱,SQL語句如下:
mysql> select distinct f_name
-> from fruits;
+------------+
| f_name |
+------------+
| apple |
| apricot |
| blackberry |
| berry |
| xxxx |
| orange |
| melon |
| cherry |
| lemon |
| mango |
| xbabay |
| xxtt |
| coconut |
| banana |
| grape |
| xbababa |
+------------+
【例3】例如,從fruits表中獲取f_name和f_price兩列,SQL語句如下:
mysql> select f_name,f_price
-> from fruits
-> ;
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| apricot | 2.20 |
| blackberry | 10.20 |
| berry | 7.60 |
| xxxx | 3.60 |
| orange | 11.20 |
| melon | 8.20 |
| cherry | 3.20 |
| lemon | 6.40 |
| mango | 15.60 |
| xbabay | 2.60 |
| xxtt | 11.60 |
| coconut | 9.20 |
| banana | 10.30 |
| grape | 5.30 |
| xbababa | 3.60 |
+------------+---------+
16 rows in set (0.00 sec)
【例4】查詢價格為10.2元的水果的名稱,SQL語句如下:
mysql> select f_name
-> from fruits
-> where f_price='10.2';
+------------+
| f_name |
+------------+
| blackberry |
+------------+
【例5】查詢名稱為“apple”的水果的價格,SQL語句如下:
mysql> select f_price from fruits where f_name='apple';
+---------+
| f_price |
+---------+
| 5.20 |
+---------+
【例6】查詢價格小於10的水果的名稱,SQL語句如下:
mysql> select f_name
-> from fruits
-> where f_price<10;
+---------+
| f_name |
+---------+
| apple |
| apricot |
| berry |
| xxxx |
| melon |
| cherry |
| lemon |
| xbabay |
| coconut |
| grape |
| xbababa |
+---------+
【例7】s_id為101和102的記錄,SQL語句如下:
mysql> select *
-> from fruits
-> where s_id=101 or s_id=102;
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b1 | 101 | blackberry | 10.20 |
| bs1 | 102 | orange | 11.20 |
| c0 | 101 | cherry | 3.20 |
| t1 | 102 | banana | 10.30 |
| t2 | 102 | grape | 5.30 |
+------+------+------------+---------+
mysql> select * from fruits where s_id in (101,102);
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b1 | 101 | blackberry | 10.20 |
| bs1 | 102 | orange | 11.20 |
| c0 | 101 | cherry | 3.20 |
| t1 | 102 | banana | 10.30 |
| t2 | 102 | grape | 5.30 |
+------+------+------------+---------+
mysql> select *from fruits where s_id regexp '10(1|2)';
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b1 | 101 | blackberry | 10.20 |
| bs1 | 102 | orange | 11.20 |
| c0 | 101 | cherry | 3.20 |
| t1 | 102 | banana | 10.30 |
| t2 | 102 | grape | 5.30 |
+------+------+------------+---------+
6 rows in set (0.00 sec)
mysql> select *from fruits where s_id regexp '10[12]';
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| a1 | 101 | apple | 5.20 |
| b1 | 101 | blackberry | 10.20 |
| bs1 | 102 | orange | 11.20 |
| c0 | 101 | cherry | 3.20 |
| t1 | 102 | banana | 10.30 |
| t2 | 102 | grape | 5.30 |
+------+------+------------+---------+
【例8】查詢所有s_id不等於101也不等於102的記錄,SQL語句如下:
mysql> select *
-> from fruits
-> where s_id not in(101,102);
+------+------+---------+---------+
| f_id | s_id | f_name | f_price |
+------+------+---------+---------+
| a2 | 103 | apricot | 2.20 |
| b2 | 104 | berry | 7.60 |
| b5 | 107 | xxxx | 3.60 |
| bs2 | 105 | melon | 8.20 |
| l2 | 104 | lemon | 6.40 |
| m1 | 106 | mango | 15.60 |
| m2 | 105 | xbabay | 2.60 |
| m3 | 105 | xxtt | 11.60 |
| o2 | 103 | coconut | 9.20 |
| t4 | 107 | xbababa | 3.60 |
+------+------+---------+---------+
mysql> select * from fruits where s_id!=101 and s_id!=102;
+------+------+---------+---------+
| f_id | s_id | f_name | f_price |
+------+------+---------+---------+
| a2 | 103 | apricot | 2.20 |
| b2 | 104 | berry | 7.60 |
| b5 | 107 | xxxx | 3.60 |
| bs2 | 105 | melon | 8.20 |
| l2 | 104 | lemon | 6.40 |
| m1 | 106 | mango | 15.60 |
| m2 | 105 | xbabay | 2.60 |
| m3 | 105 | xxtt | 11.60 |
| o2 | 103 | coconut | 9.20 |
| t4 | 107 | xbababa | 3.60 |
+------+------+---------+---------+
【例9】查詢價格在2.00元到10.20元之間的水果名稱和價格,SQL語句如下:
mysql> select f_name,f_price from fruits where 2.00<=f_price and f_price<=10.20;
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| apricot | 2.20 |
| blackberry | 10.20 |
| berry | 7.60 |
| xxxx | 3.60 |
| melon | 8.20 |
| cherry | 3.20 |
| lemon | 6.40 |
| xbabay | 2.60 |
| coconut | 9.20 |
| grape | 5.30 |
| xbababa | 3.60 |
+------------+---------+
mysql> select f_name,f_price from fruits where f_price between 2 and 10.2;
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| apricot | 2.20 |
| blackberry | 10.20 |
| berry | 7.60 |
| xxxx | 3.60 |
| melon | 8.20 |
| cherry | 3.20 |
| lemon | 6.40 |
| xbabay | 2.60 |
| coconut | 9.20 |
| grape | 5.30 |
| xbababa | 3.60 |
+------------+---------+
【例10】查詢價格在2.00元到10.20元之外的水果名稱和價格,SQL語句如下:
mysql> select f_name ,f_price from fruits where 2.00>f_price or f_price>10.20;
+--------+---------+
| f_name | f_price |
+--------+---------+
| orange | 11.20 |
| mango | 15.60 |
| xxtt | 11.60 |
| banana | 10.30 |
+--------+---------+
mysql> select f_name,f_price from fruits where not (2.00<=f_price and f_price<=10.20);
+--------+---------+
| f_name | f_price |
+--------+---------+
| orange | 11.20 |
| mango | 15.60 |
| xxtt | 11.60 |
| banana | 10.30 |
+--------+---------+
【例11】查詢所有以’b’字母開頭的水果,SQL語句如下:
mysql> select *
-> from fruits
-> where f_name like 'b%';
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
| t1 | 102 | banana | 10.30 |
+------+------+------------+---------+
mysql> select * from fruits where f_name regexp '^b.*';
mysql> select * from fruits where f_name regexp '^b';
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
| t1 | 102 | banana | 10.30 |
+------+------+------------+---------+
【例12】在fruits表中,查詢f_name中包含字母’g’的記錄,SQL語句如下:
mysql> select * from fruits where f_name like '%g%';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| bs1 | 102 | orange | 11.20 |
| m1 | 106 | mango | 15.60 |
| t2 | 102 | grape | 5.30 |
+------+------+--------+---------+
mysql> select * from fruits where f_name regexp '.*g.*';
mysql> select * from fruits where f_name regexp 'g';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| bs1 | 102 | orange | 11.20 |
| m1 | 106 | mango | 15.60 |
| t2 | 102 | grape | 5.30 |
+------+------+--------+---------+
【例13】查詢以’b’開頭,並以’y’結尾的水果的名稱,SQL語句如下:
mysql> select * from fruits where f_name like 'b%y';
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
+------+------+------------+---------+
mysql> select * from fruits where f_name regexp '^b.*y$';
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| b1 | 101 | blackberry | 10.20 |
| b2 | 104 | berry | 7.60 |
+------+------+------------+---------+
【例14】在fruits表中,查詢以字母’y’結尾,且’y’前面只有4個字母的記錄,SQL語句如下:
mysql> select *
-> from fruits
-> where f_name like '____y';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| b2 | 104 | berry | 7.60 |
+------+------+--------+---------+
mysql> select * from fruits where f_name regexp '^[a-zA-Z]{4}y$';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| b2 | 104 | berry | 7.60 |
+------+------+--------+---------+
單表查詢練習題2
CREATE TABLE customers
(
c_id int NOT NULL AUTO_INCREMENT,
c_name char(50) NOT NULL,
c_address char(50) NULL,
c_city char(50) NULL,
c_zip char(10) NULL,
c_contact char(50) NULL,
c_email char(255) NULL,
PRIMARY KEY (c_id)
);
INSERT INTO customers(c_id, c_name, c_address, c_city,
c_zip, c_contact, c_email)
VALUES(10001, 'RedHook', '200 Street ', 'Tianjin',
'300000', 'LiMing', '[email protected]'),
(10002, 'Stars', '333 Fromage Lane',
'Dalian', '116000', 'Zhangbo','[email protected]'),
(10003, 'Netbhood', '1 Sunny Place', 'Qingdao', '266000',
'LuoCong', NULL),
(10004, 'JOTO', '829 Riverside Drive', 'Haikou',
'570000', 'YangShan', '[email protected]');
【例15】查詢customers表中c_email為空的記錄的c_id、c_name和c_email欄位值,SQL語句如下:
mysql> select c_id,c_name,c_email
-> from customers
-> where c_email is null;
+-------+----------+---------+
| c_id | c_name | c_email |
+-------+----------+---------+
| 10003 | Netbhood | NULL |
+-------+----------+---------+
【例16】查詢customers表中c_email不為空的記錄的c_id、c_name和c_email欄位值,SQL語句如下:
mysql> select c_id,c_name,c_email from customers where c_email is not null;
+-------+---------+-------------------+
| c_id | c_name | c_email |
+-------+---------+-------------------+
| 10001 | RedHook | [email protected] |
| 10002 | Stars | [email protected] |
| 10004 | JOTO | [email protected] |
+-------+---------+-------------------+
【例17】在fruits表中查詢s_id = 101,並且f_price大於等於5的水果價格和名稱,SQL語句如下:
mysql> select f_name,f_price
-> from fruits
-> where s_id=101 and f_price>5;
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| blackberry | 10.20 |
+------------+---------+
【例18】在fruits表中查詢s_id = 101或者102,且f_price大於5,並且f_name=‘apple’的水果價格和名稱,SQL語句如下:
mysql> select f_name,f_price
-> from fruits
-> where (s_id=101 or s_id=102) and f_price>5;
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| blackberry | 10.20 |
| orange | 11.20 |
| banana | 10.30 |
| grape | 5.30 |
+------------+---------+
mysql> select f_name,f_price from fruits where s_id in(101,102) and f_price>5;
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| blackberry | 10.20 |
| orange | 11.20 |
| banana | 10.30 |
| grape | 5.30 |
+------------+---------+
【例19】查詢s_id=101或者s_id=102的水果供應商的f_price和f_name,SQL語句如下:
mysql> select f_price,f_name
-> from fruits
-> where s_id=101 or s_id=102;
+---------+------------+
| f_price | f_name |
+---------+------------+
| 5.20 | apple |
| 10.20 | blackberry |
| 11.20 | orange |
| 3.20 | cherry |
| 10.30 | banana |
| 5.30 | grape |
+---------+------------+
【例20】查詢s_id=101或者s_id=102的水果供應商的f_price和f_name,SQL語句如下:
mysql> select f_price,f_name from fruits where s_id in (101,102);
+---------+------------+
| f_price | f_name |
+---------+------------+
| 5.20 | apple |
| 10.20 | blackberry |
| 11.20 | orange |
| 3.20 | cherry |
| 10.30 | banana |
| 5.30 | grape |
+---------+------------+
【例21】查詢fruits表中s_id欄位的值,返回s_id欄位值且不得重複,SQL語句如下:
mysql> select distinct s_id
-> from fruits;
+------+
| s_id |
+------+
| 101 |
| 103 |
| 104 |
| 107 |
| 102 |
| 105 |
| 106 |
+------+
【例22】查詢fruits表的f_name欄位值,並對其進行排序,SQL語句如下:
mysql> select f_name
-> from fruits
-> order by f_name;
+------------+
| f_name |
+------------+
| apple |
| apricot |
| banana |
| berry |
| blackberry |
| cherry |
| coconut |
| grape |
| lemon |
| mango |
| melon |
| orange |
| xbababa |
| xbabay |
| xxtt |
| xxxx |
+------------+
【例23】查詢fruits表中的f_name和f_price欄位,先按f_name排序,再按f_price排序,SQL語句如下:
mysql> select f_name,f_price from fruits order by f_name,f_price;
+------------+---------+
| f_name | f_price |
+------------+---------+
| apple | 5.20 |
| apricot | 2.20 |
| banana | 10.30 |
| berry | 7.60 |
| blackberry | 10.20 |
| cherry | 3.20 |
| coconut | 9.20 |
| grape | 5.30 |
| lemon | 6.40 |
| mango | 15.60 |
| melon | 8.20 |
| orange | 11.20 |
| xbababa | 3.60 |
| xbabay | 2.60 |
| xxtt | 11.60 |
| xxxx | 3.60 |
+------------+---------+
【例24】查詢fruits表中的f_name和f_price欄位,對結果按f_price降序方式排序,SQL語句如下:
mysql> select f_name,f_price from fruits order by f_price desc;
+------------+---------+
| f_name | f_price |
+------------+---------+
| mango | 15.60 |
| xxtt | 11.60 |
| orange | 11.20 |
| banana | 10.30 |
| blackberry | 10.20 |
| coconut | 9.20 |
| melon | 8.20 |
| berry | 7.60 |
| lemon | 6.40 |
| grape | 5.30 |
| apple | 5.20 |
| xxxx | 3.60 |
| xbababa | 3.60 |
| cherry | 3.20 |
| xbabay | 2.60 |
| apricot | 2.20 |
+------------+---------+
【例25】查詢fruits表,先按f_price降序排序,再按f_name欄位升序排序,SQL語句如下:
mysql> select * from fruits order by f_price desc,f_name;
+------+------+------------+---------+
| f_id | s_id | f_name | f_price |
+------+------+------------+---------+
| m1 | 106 | mango | 15.60 |
| m3 | 105 | xxtt | 11.60 |
| bs1 | 102 | orange | 11.20 |
| t1 | 102 | banana | 10.30 |
| b1 | 101 | blackberry | 10.20 |
| o2 | 103 | coconut | 9.20 |
| bs2 | 105 | melon | 8.20 |
| b2 | 104 | berry | 7.60 |
| l2 | 104 | lemon | 6.40 |
| t2 | 102 | grape | 5.30 |
| a1 | 101 | apple | 5.20 |
| t4 | 107 | xbababa | 3.60 |
| b5 | 107 | xxxx | 3.60 |
| c0 | 101 | cherry | 3.20 |
| m2 | 105 | xbabay | 2.60 |
| a2 | 103 | apricot | 2.20 |
+------+------+------------+---------+
【例26】根據s_id對fruits表中的資料進行分組,SQL語句如下:
mysql> select max(f_price),min(f_price),avg(f_price) from fruits group by s_id;
+--------------+--------------+--------------+
| max(f_price) | min(f_price) | avg(f_price) |
+--------------+--------------+--------------+
| 10.20 | 3.20 | 6.200000 |
| 11.20 | 5.30 | 8.933333 |
| 9.20 | 2.20 | 5.700000 |
| 7.60 | 6.40 | 7.000000 |
| 11.60 | 2.60 | 7.466667 |
| 15.60 | 15.60 | 15.600000 |
| 3.60 | 3.60 | 3.600000 |
+--------------+--------------+--------------+
【例27】根據s_id對fruits表中的資料進行分組,將每個供應商的水果名稱顯示出來,SQL語句如下:
mysql> select s_id,group_concat(f_name) from fruits group by s_id;
+------+-------------------------+
| s_id | group_concat(f_name) |
+------+-------------------------+
| 101 | apple,blackberry,cherry |
| 102 | orange,banana,grape |
| 103 | apricot,coconut |
| 104 | berry,lemon |
| 105 | melon,xbabay,xxtt |
| 106 | mango |
| 107 | xxxx,xbababa |
+------+-------------------------+
【例28】根據s_id對fruits表中的資料進行分組,並顯示水果種類大於1的分組資訊,SQL語句如下:
mysql> select s_id,count(1) from fruits group by s_id having count(1)>1;
+------+----------+
| s_id | count(1) |
+------+----------+
| 101 | 3 |
| 102 | 3 |
| 103 | 2 |
| 104 | 2 |
| 105 | 3 |
| 107 | 2 |
+------+----------+
【例29】根據s_id對fruits表中的資料進行分組,並顯示記錄數量,SQL語句如下:
mysql> select s_id,count(1) from fruits group by s_id;
+------+----------+
| s_id | count(1) |
+------+----------+
| 101 | 3 |
| 102 | 3 |
| 103 | 2 |
| 104 | 2 |
| 105 | 3 |
| 106 | 1 |
| 107 | 2 |
+------+----------+
【例30】根據s_id和f_name欄位對fruits表中的資料進行分組, SQL語句如下,
mysql> select s_id,f_name from fruits
-> group by s_id,f_name;
+------+------------+
| s_id | f_name |
+------+------------+
| 101 | apple |
| 101 | blackberry |
| 101 | cherry |
| 102 | banana |
| 102 | grape |
| 102 | orange |
| 103 | apricot |
| 103 | coconut |
| 104 | berry |
| 104 | lemon |
| 105 | melon |
| 105 | xbabay |
| 105 | xxtt |
| 106 | mango |
| 107 | xbababa |
| 107 | xxxx |
+------+------------+
單表查詢練習題3
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cs_id` int(11) NOT NULL COMMENT '課程編號',
`cs_name` varchar(50) NOT NULL COMMENT '課程名稱',
`cs_credit` tinyint(255) unsigned DEFAULT NULL COMMENT '課程學分',
`cs_type` char(12) DEFAULT NULL COMMENT '課程類別',
`cs_depart` char(6) DEFAULT NULL COMMENT '院系名稱',
PRIMARY KEY (`cs_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `course`(`cs_id`,`cs_name`,`cs_credit`,`cs_type`,`cs_depart`) values (5200313,'資料庫原理及應用',4,'核心專業','信工'),(5203314,'計算機導論',4,'通識教育','信工'),(5219314,'資料結構',5,'專業核心','信工'),(5223013,'大學物理',4,'專業基礎','信工'),(5227614,'畢業實習',4,'集中實踐','信工'),(5230912,'雲端計算',2,'共同選修','信工'),(5236212,'機器學習',2,'共同選修','信工'),(5237514,'c語言',4,'專業基礎','信工'),(5245112,'區塊鏈',2,'任意選修','信工'),(7200422,'智慧財產權法',2,'任意選修','文法'),(20201833,'概率論',3,'專業基礎','基礎'),(20202336,'高等數學',6,'專業基礎','基礎'),(29299131,'勞動教育',1,'集中實踐','學務');
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`stu_id` bigint(11) unsigned NOT NULL COMMENT '學號',
`stu_name` char(12) NOT NULL COMMENT '姓名',
`stu_sex` enum('男','女') DEFAULT NULL COMMENT '性別',
`stu_age` tinyint(255) unsigned DEFAULT NULL COMMENT '年齡',
`stu_major` char(9) DEFAULT NULL COMMENT '專業',
`stu_college` char(12) DEFAULT NULL COMMENT '學院',
PRIMARY KEY (`stu_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `student`(`stu_id`,`stu_name`,`stu_sex`,`stu_age`,`stu_major`,`stu_college`) values (201804550101,'郭奎','男',22,'計科','信工學院'),(201804550102,'呂宇航','男',18,'計科','信工學院'),(201804550103,'張豪輝','女',19,'計科','信工學院'),(201804550107,'丁志傑','男',17,'金融學','金貿學院'),(201804550109,'範偉','男',19,'金融學','金貿學院'),(201804550116,'張依婷','女',17,'大資料','信工學院'),(201804550120,'張維','男',19,'計科','信工學院'),(201804550121,'朱柳陽','女',20,'計科','信工學院'),(201804550144,'譚兵炎','男',20,'大資料','信工學院'),(201804550153,'楊志強','男',17,'大資料','信工學院');
1.查詢全部課程的資訊。
mysql> select * from course;
+----------+--------------------------+-----------+--------------+-----------+
| cs_id | cs_name | cs_credit | cs_type | cs_depart |
+----------+--------------------------+-----------+--------------+-----------+
| 5200313 | 資料庫原理及應用 | 4 | 核心專業 | 信工 |
| 5203314 | 計算機導論 | 4 | 通識教育 | 信工 |
| 5219314 | 資料結構 | 5 | 專業核心 | 信工 |
| 5223013 | 大學物理 | 4 | 專業基礎 | 信工 |
| 5227614 | 畢業實習 | 4 | 集中實踐 | 信工 |
| 5230912 | 雲端計算 | 2 | 共同選修 | 信工 |
| 5236212 | 機器學習 | 2 | 共同選修 | 信工 |
| 5237514 | c語言 | 4 | 專業基礎 | 信工 |
| 5245112 | 區塊鏈 | 2 | 任意選修 | 信工 |
| 7200422 | 智慧財產權法 | 2 | 任意選修 | 文法 |
| 20201833 | 概率論 | 3 | 專業基礎 | 基礎 |
| 20202336 | 高等數學 | 6 | 專業基礎 | 基礎 |
| 29299131 | 勞動教育 | 1 | 集中實踐 | 學務 |
+----------+--------------------------+-----------+--------------+-----------+
2.查詢信工學院開設的課程名、課程號及學分。
mysql> select cs_id,cs_name,cs_credit from course where cs_depart='信工';
+---------+--------------------------+-----------+
| cs_id | cs_name | cs_credit |
+---------+--------------------------+-----------+
| 5200313 | 資料庫原理及應用 | 4 |
| 5203314 | 計算機導論 | 4 |
| 5219314 | 資料結構 | 5 |
| 5223013 | 大學物理 | 4 |
| 5227614 | 畢業實習 | 4 |
| 5230912 | 雲端計算 | 2 |
| 5236212 | 機器學習 | 2 |
| 5237514 | c語言 | 4 |
| 5245112 | 區塊鏈 | 2 |
+---------+--------------------------+-----------+
3.查詢學分超過3學分的課程程式碼、課程名和開課單位。
mysql> select cs_id,cs_name,cs_depart
-> from course
-> where cs_credit>3;
+----------+--------------------------+-----------+
| cs_id | cs_name | cs_depart |
+----------+--------------------------+-----------+
| 5200313 | 資料庫原理及應用 | 信工 |
| 5203314 | 計算機導論 | 信工 |
| 5219314 | 資料結構 | 信工 |
| 5223013 | 大學物理 | 信工 |
| 5227614 | 畢業實習 | 信工 |
| 5237514 | c語言 | 信工 |
| 20202336 | 高等數學 | 基礎 |
+----------+--------------------------+-----------+
4.查詢計科專業和大資料專業的學生資訊。
mysql> select stu_id,stu_name from student where stu_major='計科' or stu_major='大資料';
+--------------+-----------+
| stu_id | stu_name |
+--------------+-----------+
| 201804550101 | 郭奎 |
| 201804550102 | 呂宇航 |
| 201804550103 | 張豪輝 |
| 201804550116 | 張依婷 |
| 201804550120 | 張維 |
| 201804550121 | 朱柳陽 |
| 201804550144 | 譚兵炎 |
| 201804550153 | 楊志強 |
+--------------+-----------+
5.查詢不是信工學院的學生姓名和學號。
mysql> select stu_id,stu_name
-> from student
-> where stu_college !='信工學院';
+--------------+-----------+
| stu_id | stu_name |
+--------------+-----------+
| 201804550107 | 丁志傑 |
| 201804550109 | 範偉 |
+--------------+-----------+
6.查詢年齡是17,18,19的學生姓名和專業。
mysql> select stu_name,stu_major from student where stu_age in (17,18,19);
+-----------+-----------+
| stu_name | stu_major |
+-----------+-----------+
| 呂宇航 | 計科 |
| 張豪輝 | 計科 |
| 丁志傑 | 金融學 |
| 範偉 | 金融學 |
| 張依婷 | 大資料 |
| 張維 | 計科 |
| 楊志強 | 大資料 |
+-----------+-----------+
7.查詢學分在2到4之間課程的資訊。 *****
mysql> select cs_name
-> from course
-> where cs_credit>2 and cs_credit<4;
+-----------+
| cs_name |
+-----------+
| 概率論 |
+-----------+
8.查詢課程名稱中帶“資料”的課程名、課程號及開課單位。
mysql> select cs_id,cs_name,cs_depart
-> from course
-> where cs_name like '%資料%';
+---------+--------------------------+-----------+
| cs_id | cs_name | cs_depart |
+---------+--------------------------+-----------+
| 5200313 | 資料庫原理及應用 | 信工 |
| 5219314 | 資料結構 | 信工 |
+---------+--------------------------+-----------+
9.查詢信工學院的的專業有哪些。
mysql> select distinct cs_type
-> from course
-> where cs_depart='信工';
+--------------+
| cs_type |
+--------------+
| 核心專業 |
| 通識教育 |
| 專業核心 |
| 專業基礎 |
| 集中實踐 |
| 共同選修 |
| 任意選修 |
+--------------+
10.查詢年齡為空的學生資訊。
mysql> select *
-> from student
-> where stu_age is null;
Empty set (0.00 sec)
11.查詢不是信工學院開設的集中實踐課的開課單位和課程名稱。
mysql> select cs_name,cs_depart
-> from course
-> where cs_depart !='信工' and cs_type='集中實踐';
+--------------+-----------+
| cs_name | cs_depart |
+--------------+-----------+
| 勞動教育 | 學務 |
+--------------+-----------+
12.查詢信工學院開設的課程的型別有哪些。
mysql> select distinct cs_type
-> from course
-> where cs_depart='信工';
+--------------+
| cs_type |
+--------------+
| 核心專業 |
| 通識教育 |
| 專業核心 |
| 專業基礎 |
| 集中實踐 |
| 共同選修 |
| 任意選修 |
+--------------+
13.查詢學生所在的專業個數。
mysql> select stu_major,count(stu_major) from student group by stu_major;
+-----------+------------------+
| stu_major | count(stu_major) |
+-----------+------------------+
| 大資料 | 3 |
| 計科 | 5 |
| 金融學 | 2 |
+-----------+------------------+
14.查詢信工學院開設的課程的平均學分。
mysql> select avg(cs_credit)
-> from course
-> where cs_depart='信工';
+----------------+
| avg(cs_credit) |
+----------------+
| 3.4444 |
+----------------+
15.查詢學生的資訊,查詢結果按姓名升序排序。
mysql> select *
-> from student
-> order by stu_name;
+--------------+-----------+---------+---------+-----------+--------------+
| stu_id | stu_name | stu_sex | stu_age | stu_major | stu_college |
+--------------+-----------+---------+---------+-----------+--------------+
| 201804550107 | 丁志傑 | 男 | 17 | 金融學 | 金貿學院 |
| 201804550102 | 呂宇航 | 男 | 18 | 計科 | 信工學院 |
| 201804550116 | 張依婷 | 女 | 17 | 大資料 | 信工學院 |
| 201804550120 | 張維 | 男 | 19 | 計科 | 信工學院 |
| 201804550103 | 張豪輝 | 女 | 19 | 計科 | 信工學院 |
| 201804550121 | 朱柳陽 | 女 | 20 | 計科 | 信工學院 |
| 201804550153 | 楊志強 | 男 | 17 | 大資料 | 信工學院 |
| 201804550109 | 範偉 | 男 | 19 | 金融學 | 金貿學院 |
| 201804550144 | 譚兵炎 | 男 | 20 | 大資料 | 信工學院 |
| 201804550101 | 郭奎 | 男 | 22 | 計科 | 信工學院 |
+--------------+-----------+---------+---------+-----------+--------------+
16.查詢每個專業的學生的最大年齡、最小年齡和平均年齡,查詢結果按平均年齡降序排列。
mysql> select max(stu_age),min(stu_age),avg(stu_age)
-> from student
-> group by stu_college
-> order by avg(stu_age)desc;
+--------------+--------------+--------------+
| max(stu_age) | min(stu_age) | avg(stu_age) |
+--------------+--------------+--------------+
| 22 | 17 | 19.0000 |
| 19 | 17 | 18.0000 |
+--------------+--------------+--------------+
17.查詢每個開課單位開設的課程門數的,查詢結果按課程門數升序排列。
mysql> select count(cs_name)
-> from course
-> group by cs_depart
-> order by count(cs_name) desc;
+----------------+
| count(cs_name) |
+----------------+
| 9 |
| 2 |
| 1 |
| 1 |
+----------------+
18.查詢單位開課門數少於2門的開課單位和課程名稱。