數據庫的CRUD之Retrieve

語法

SELECT [DISTINCT] {*|[,column]} FROM table_name
							[WHERE...]
							[ORDER BY column [ASC|DESC],...]
							[LIMIT]...

操作的數據庫表

mysql> CREATE TABLE exam_score(
    -> id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    -> name VARCHAR(20) NOT NULL COMMENT '同學姓名',
    -> yuwen float DEFAULT 0.0 COMMENT '語⽂成績',
    -> shuxue float DEFAULT 0.0 COMMENT '數學成績',
    -> yingyu float DEFAULT 0.0 COMMENT '英語成績'
    -> );
Query OK, 0 rows affected (0.45 sec)

--插入數據:
 insert into exam_score(name,yuwen,shuxue,yingyu) values
    -> ('蔡文姬',68,95,57),
    -> ('李白',84,76,74),
    -> ('虞姬',88,85,62),
    -> ('韓信',85,97,93),
    -> ('諸葛亮',54,87,46),
    -> ('貂蟬',77,75,78),
    -> ('小喬',76,66,36);

SLECT

1.全列查詢
通常不建議直接使用*進行查詢,因爲:
1.查詢的列越多,數據庫需要輸出的數據量越大,會卡數據庫。
2.可能會影響到索引的使用。

mysql> select * from exam_score;
+----+--------+-------+--------+--------+
| id | name   | yuwen | shuxue | yingyu |
+----+--------+-------+--------+--------+
|  1 | 蔡文姬 |    68 |     95 |     57 |
|  2 | 李白   |    84 |     76 |     74 |
|  3 | 虞姬   |    88 |     85 |     62 |
|  4 | 韓信   |    85 |     97 |     93 |
|  5 | 諸葛亮 |    54 |     87 |     46 |
|  6 | 貂蟬   |    77 |     75 |     78 |
|  7 | 小喬   |    76 |     66 |     36 |
+----+--------+-------+--------+--------+
7 rows in set (0.00 sec)

2.指定列查詢

mysql> select name,yuwen,shuxue from exam_score;
+--------+-------+--------+
| name   | yuwen | shuxue |
+--------+-------+--------+
| 蔡文姬 |    68 |     95 |
| 李白   |    84 |     76 |
| 虞姬   |    88 |     85 |
| 韓信   |    85 |     97 |
| 諸葛亮 |    54 |     87 |
| 貂蟬   |    77 |     75 |
| 小喬   |    76 |     66 |
+--------+-------+--------+
7 rows in set (0.00 sec)

3.查詢字段爲表達式
3.1表達式不包含字段

mysql> select name,yuwen,10 from exam_score;
+--------+-------+----+
| name   | yuwen | 10 |
+--------+-------+----+
| 蔡文姬 |    68 | 10 |
| 李白   |    84 | 10 |
| 虞姬   |    88 | 10 |
| 韓信   |    85 | 10 |
| 諸葛亮 |    54 | 10 |
| 貂蟬   |    77 | 10 |
| 小喬   |    76 | 10 |
+--------+-------+----+
7 rows in set (0.02 sec)

3.2表達式包含一個字段

mysql> select name,yuwen,shuxue+10 from exam_score;
+--------+-------+-----------+
| name   | yuwen | shuxue+10 |
+--------+-------+-----------+
| 蔡文姬 |    68 |       105 |
| 李白   |    84 |        86 |
| 虞姬   |    88 |        95 |
| 韓信   |    85 |       107 |
| 諸葛亮 |    54 |        97 |
| 貂蟬   |    77 |        85 |
| 小喬   |    76 |        76 |
+--------+-------+-----------+
7 rows in set (0.09 sec)

3.3表達式包含多個字段

mysql> select name,yuwen+shuxue+yingyu from exam_score;
+--------+---------------------+
| name   | yuwen+shuxue+yingyu |
+--------+---------------------+
| 蔡文姬 |                 220 |
| 李白   |                 234 |
| 虞姬   |                 235 |
| 韓信   |                 275 |
| 諸葛亮 |                 187 |
| 貂蟬   |                 230 |
| 小喬   |                 178 |
+--------+---------------------+
7 rows in set (0.00 sec)

4.爲查詢結果指定列名
語法:

SELECT column [AS] clo_name[...] FROM table_name;

使用:

mysql> select name,yuwen+shuxue+yingyu as '總分' from exam_score;
+--------+------+
| name   | 總分 |
+--------+------+
| 蔡文姬 |  220 |
| 李白   |  234 |
| 虞姬   |  235 |
| 韓信   |  275 |
| 諸葛亮 |  187 |
| 貂蟬   |  230 |
| 小喬   |  178 |
+--------+------+
7 rows in set (0.03 sec)

5.結果去重
語法:

SELECT DISTINCT column FORM table_name;

WHERE

比較運算符:

比較運算符 語義
>,>=,<,<= 大於,大於等於,小於,小於等於
= 等於,NULL不安全,NULL=NULL,返回NULL
<=> NULL安全,NULL<=>NULL,返回TRUE(1)
!=,<> 不等於
BETWEEN a AND b 範圍匹配[a,b],在範圍內返回TRUE(1)
IN(option,…) 如果是option中任意一個,返回true
IS NULL 空判斷
IS NOT NULL 非空判斷
LIKE 模糊匹配,%表示任意多個(包含0個)任意字符;_表示一個字符

邏輯運算符

邏輯運算符 語義
AND 與,都爲TRUE(1),結果才爲TRUE(1)
OR 或,至少一個爲TRUE(1),結果就爲TRUE(1)
NOT 反,條件爲TRUE(1),結果爲FALSE(0)

爲了減少篇幅,方便觀看,從這裏之後的語句將不再顯示出運行結果,但是所有的語句都是結果正確的
1.英語不及格的同學及英語成績

select name,yingyu from exam_score where yingyu<60;

2.語文成績在80-90間的同學的語文成績

--使用between...and...運算符
 select name,yuwen from exam_score where yuwen between 80 and 90;
 --使用and運算符
 select name,yuwen from exam_score where yuwen>=80 and yuwen<=90;

3.數學成績在55或56或95或96的同學

--使用or運算符
select name,shuxue from exam_score where shuxue=55 or shuxue=56
    -> or shuxue=95 or shuxue=96;
--使用in運算符
select name,shuxue from exam_score where shuxue in(55,56,95,96);

4.姓蔡的同學及李某同學

select name from exam_score where name like '蔡%' 
	->or name like '李_';

5.語文成績好於英語成績的同學

 select name,yuwen,yingyu from exam_score where yuwen>yingyu;

6.總分在200分以下的同學注:別名不可用在表達式中

--where條件中使用表達式,別名不可用在表達式中
select name,yuwen+shuxue+yingyu as 總分 from exam_score 
	->where yuwen+shuxue+yingyu<200;

7.語文成績大於80,並且不姓韓的同學

select name,yuwen from exam_score where yuwen>80
    -> and name not like '韓%';

8.韓某同學,否則總成績>200並且語文<數學並且英語大於80–綜合性查詢

select * from exam_score where name like '韓_' or(
    -> yuwen<shuxue and yingyu>80);

9.NULL查詢

9.1查詢語文成績不爲空的同學
select name,yuwen from exam_score where yuwen is not null;

9.2NULL和NULL比較,=<=>的區別
mysql> select null=null,null=1,null=2;
+-----------+--------+--------+
| null=null | null=1 | null=2 |
+-----------+--------+--------+
|      NULL |   NULL |   NULL |
+-----------+--------+--------+
1 row in set (0.00 sec)

mysql> select null<=>null,null<=>1,null<=>2;
+-------------+----------+----------+
| null<=>null | null<=>1 | null<=>2 |
+-------------+----------+----------+
|           1 |        0 |        0 |
+-------------+----------+----------+
1 row in set (0.00 sec)

ORDER BY

ASC:升序(默認)
DESC:降序
注:沒有OEDER BY子句的查詢,返回的結果是未定義的,永遠都不要依賴於這個順序。
1.同學及數學成績,按數學成績升序顯示

select name,shuxue from exam_score order by shuxue;

2.查看同學的各門成績,依次按數學降序,語文升序,英語升序顯示

select * from exam_score order by shuxue desc,yuwen,yingyu;
--只有數學排序規範

3.查學生總分,按總分由高到低排列
order by子句可以使用別名。

SELECT name, yuwen + yingyu + shuxue 總分 FROM exam_score
    -> ORDER BY 總分 DESC;

4.查詢姓李的和姓虞的同學的數學成績,由高到低顯示

 select name,shuxue from exam_score order by shuxue desc;

LIMIT

語法:

--起始下標爲0
1.0開始選n條。
SELECT...FROM table_name [WHERE...] [ORDER BY...] LIMIT n;
2.從s開始選n條:
SELECT...FROM table_name [WHERE...] [ORDER BY...] LIMIT s,n;
3.從s開始選n條,和2類似,但比2更明確:
SELECT...FROM table_name [WHERE...] [ORDER BY...] LIMIT n OFFSET s;

當查看未知表時,建議先加一個limit1,避免因表中數據太大,造成數據庫卡死現象。
使用:
按照id進行分頁,每頁三條信息(最後一頁不足三個也不會出錯)

--第1頁
mysql> select * from exam_score limit 3;
+----+--------+-------+--------+--------+
| id | name   | yuwen | shuxue | yingyu |
+----+--------+-------+--------+--------+
|  1 | 蔡文姬 |    68 |     95 |     57 |
|  2 | 李白   |    84 |     76 |     74 |
|  3 | 虞姬   |    88 |     85 |     62 |
+----+--------+-------+--------+--------+
3 rows in set (0.00 sec)

--第二頁
mysql> select * from exam_score limit 3,3;
+----+--------+-------+--------+--------+
| id | name   | yuwen | shuxue | yingyu |
+----+--------+-------+--------+--------+
|  4 | 韓信   |    85 |     97 |     93 |
|  5 | 諸葛亮 |    54 |     87 |     46 |
|  6 | 貂蟬   |    77 |     75 |     78 |
+----+--------+-------+--------+--------+
3 rows in set (0.00 sec)

--第三頁
mysql> select * from exam_score limit 3 offset 6;
+----+------+-------+--------+--------+
| id | name | yuwen | shuxue | yingyu |
+----+------+-------+--------+--------+
|  7 | 小喬 |    76 |     66 |     36 |
+----+------+-------+--------+--------+
1 row in set (0.00 sec)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章