數據庫查詢練習-簡單查詢練習及筆記(1)
條件查詢 where
1. = != <> > >= <= 算數運算符
2.betwoon ....and... 區間查詢
3. in 查詢指定字段多個值
4. is null 判斷是空
5.and 連接 ..和..
6. or 並且
7. not
1查詢表中所有記錄
查詢student表所有記錄
select * from student;
# *表示所有字段
/*
返回如下結果
mysql> select * from student;
+-----+--------+------+---------------------+--------+
| sno | sname | ssex | sbirthday | sclass |
+-----+--------+------+---------------------+--------+
| 101 | 曾華 | 男 | 1977-09-01 00:00:00 | 95033 |
| 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 103 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 |
| 104 | 李軍 | 男 | 1976-02-20 00:00:00 | 95033 |
| 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 106 | 陸軍 | 男 | 1974-06-03 00:00:00 | 95031 |
| 107 | 王尼瑪 | 男 | 1976-02-20 00:00:00 | 95033 |
| 108 | 張全蛋 | 男 | 1975-02-10 00:00:00 | 95031 |
| 109 | 趙鐵柱 | 男 | 1974-06-03 00:00:00 | 95031 |
+-----+--------+------+---------------------+--------+
9 rows in set (0.04 sec)
*/
2查詢指定字段
查詢 student 表中的 sname、ssex 和 sclass 字段的所有行
select sname,ssex,sclass from student;
#select 字段,字段,字段 from 表名;
/*
mysql> select sname,ssex,sclass from student;
+--------+------+--------+
| sname | ssex | sclass |
+--------+------+--------+
| 曾華 | 男 | 95033 |
| 匡明 | 男 | 95031 |
| 王麗 | 女 | 95033 |
| 李軍 | 男 | 95033 |
| 王芳 | 女 | 95031 |
| 陸軍 | 男 | 95031 |
| 王尼瑪 | 男 | 95033 |
| 張全蛋 | 男 | 95031 |
| 趙鐵柱 | 男 | 95031 |
+--------+------+--------+
9 rows in set (0.05 sec)
*/
3 distinct: 去重查詢
查詢 teacher 表中不重複的 depart 列
#未去重查詢前
select depart from teacher;
#select 字段 from 表名;
/*
mysql> select depart from teacher;
+------------+
| depart |
+------------+
| 計算機系 |
| 計算機系 |
| 電子工程系 |
| 電子工程系 |
+------------+
4 rows in set (0.06 sec)
*/
select distinct depart from teacher;
#elect distinct 字段名 from 表名;
#去重查詢後distinct
/*
mysql> select distinct depart from teacher;
+------------+
| depart |
+------------+
| 計算機系 |
| 電子工程系 |
+------------+
2 rows in set (0.06 sec)
*/
4 查詢區間
查詢 score 表中成績在60-80之間的所有行
select * from score where degree between 60 and 80;
# select * from 表名 where 字段名 between 大於多少 and 小於多少;
/*
mysql> select * from score where degree between 60 and 80;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
| 109 | 3-105 | 76 |
| 109 | 3-245 | 68 |
+-----+-------+--------+
4 rows in set (0.02 sec)
*/
5 運算符查詢
查詢 score 表中成績在60-80之間的所有行
select * from score where degree > 60 and degree <80;
# select * from 表名稱 where 字段 > 範圍值 and 字段 <範圍值;
/*
mysql> select * from score where degree > 60 and degree <80;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
| 109 | 3-105 | 76 |
| 109 | 3-245 | 68 |
+-----+-------+--------+
4 rows in set (0.03 sec)
*/
6 表示或者關係的查詢(同一字段中查詢)
查詢 score 表中成績爲 85, 86 或 88 的行
IN: 查詢規定中的多個值
not in 表示反選in值
select * from score where degree in(85,86,88);
select * from score where degree not in(85,86,88);
# select * from 表名 where 字段名 in(值1,值2,值3);
/*
mysql> select * from score where degree in(85,86,88);
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-245 | 86 |
| 103 | 6-166 | 85 |
| 105 | 3-105 | 88 |
+-----+-------+--------+
3 rows in set (0.03 sec)
*/
7 表示或者關係的查詢(多個字段中查詢)
查詢 student 表中 ‘95031’ 班或性別爲 ‘女’ 的所有行
or: 表示或者關係
select * from student where sclass='95031' or ssex='女';
# select * from 表名 where 字段1='字段1下的值' or 字段2='字段2下的值';
/*
mysql> select * from student where sclass='95031' or ssex='女';
+-----+--------+------+---------------------+--------+
| sno | sname | ssex | sbirthday | sclass |
+-----+--------+------+---------------------+--------+
| 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 103 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 |
| 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 106 | 陸軍 | 男 | 1974-06-03 00:00:00 | 95031 |
| 108 | 張全蛋 | 男 | 1975-02-10 00:00:00 | 95031 |
| 109 | 趙鐵柱 | 男 | 1974-06-03 00:00:00 | 95031 |
+-----+--------+------+---------------------+--------+
6 rows in set (0.03 sec)
*/
8 升序排列 降序排列
– 以 class 降序的方式查詢 student 表的所有行
– DESC: 降序,從高到低
– ASC(默認): 升序,從低到高
# 降序排列
select * from student order by sclass desc;
# select * from 表名 order by 字段名 desc;
/*
mysql> select * from student order by sclass desc;
+-----+--------+------+---------------------+--------+
| sno | sname | ssex | sbirthday | sclass |
+-----+--------+------+---------------------+--------+
| 101 | 曾華 | 男 | 1977-09-01 00:00:00 | 95033 |
| 103 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 |
| 104 | 李軍 | 男 | 1976-02-20 00:00:00 | 95033 |
| 107 | 王尼瑪 | 男 | 1976-02-20 00:00:00 | 95033 |
| 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 106 | 陸軍 | 男 | 1974-06-03 00:00:00 | 95031 |
| 108 | 張全蛋 | 男 | 1975-02-10 00:00:00 | 95031 |
| 109 | 趙鐵柱 | 男 | 1974-06-03 00:00:00 | 95031 |
+-----+--------+------+---------------------+--------+
9 rows in set (0.03 sec)
*/
# 升序排列(在排序時如果不指定排序規則,默認升序)
select * from student order by sclass asc;
# select * from 表名 order by 字段名 asc;
/*
mysql> select * from student order by sclass asc;
+-----+--------+------+---------------------+--------+
| sno | sname | ssex | sbirthday | sclass |
+-----+--------+------+---------------------+--------+
| 102 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 106 | 陸軍 | 男 | 1974-06-03 00:00:00 | 95031 |
| 108 | 張全蛋 | 男 | 1975-02-10 00:00:00 | 95031 |
| 109 | 趙鐵柱 | 男 | 1974-06-03 00:00:00 | 95031 |
| 101 | 曾華 | 男 | 1977-09-01 00:00:00 | 95033 |
| 103 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 |
| 104 | 李軍 | 男 | 1976-02-20 00:00:00 | 95033 |
| 107 | 王尼瑪 | 男 | 1976-02-20 00:00:00 | 95033 |
+-----+--------+------+---------------
*/
#不指定排序規則
select * from student order by sclass;
#返回結果與升序排列結果一樣
9 一個字段升序 ,一個字段降序
以 cno 升序、degree 降序查詢 score 表的所有行
select * from score order by cno asc,degree desc;
# select * from 表名 order by 字段名1 asc,字段名2 desc;
#在查詢時先按字段1排序,當字段一一樣時,在找字段2排序
/*
mysql> select * from score order by cno asc,degree desc;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
| 105 | 3-105 | 88 |
| 109 | 3-105 | 76 |
| 103 | 3-245 | 86 |
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 103 | 6-166 | 85 |
| 109 | 6-166 | 81 |
| 105 | 6-166 | 79 |
+-----+-------+--------+
9 rows in set (0.02 sec)
*/
10 統計
– 查詢 “95031” 班的學生人數
– COUNT: 統計
select count(*) from student where sclass='95031';
# select count(*) from 表名 where 統計的字段名='統計的值';
#返回查詢到的總和數
/*
mysql> select count(*) from student where sclass='95031';
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.02 sec)
*/
11 查詢最大,最小值
max 最大值
min 最小值
select max(degree) from score;
#select max(查詢字段) from 查詢那個表;
/*
mysql> select max(degree) from score;
+-------------+
| max(degree) |
+-------------+
| 92 |
+-------------+
1 row in set (0.02 sec)
*/
12 子查詢
– 查詢 score 表中的最高分的學生學號和課程編號(子查詢或排序查詢)。
– (SELECT MAX(degree) FROM score): 子查詢,算出最高分
select sno,cno from score where degree=(select max(degree) from score);
#select 查詢字段1,查詢字段2 from score where 查詢條件字段=查詢條件字段對應的值;
#select 查詢字段1,查詢字段2 from score where 查詢條件字段=(select max(字段名) from 表名);
/*
mysql> select sno,cno from score where degree=(select max(degree) from score);
+-----+-------+
| sno | cno |
+-----+-------+
| 103 | 3-105 |
+-----+-------+
1 row in set (0.01 sec)
*/
13 分頁查詢
limit 0,1
select * from score limit 3,5;
#select * from 表名 limit 從第幾條開始查(不包括當前),查詢幾條數據;
/*
mysql> select * from score limit 3,5;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 105 | 3-105 | 88 |
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
| 109 | 3-105 | 76 |
| 109 | 3-245 | 68 |
+-----+-------+--------+
5 rows in set (0.03 sec)
*/
14 查詢空數據
字段is null 空
字段is not null;不爲空
not 字段 is null;不爲空
select * from score where cno is null;
#select * from 表名 where 字段 is null;