wordpress最新版下載2022網(wǎng)站seo
文章目錄
- 數(shù)據(jù)的查詢(補(bǔ)充)
- 條件查詢
- 關(guān)于SQL語句的執(zhí)行順序
- 分頁查詢(LIMIT)
- 數(shù)據(jù)的修改
- 數(shù)據(jù)修改基礎(chǔ)知識
數(shù)據(jù)的查詢(補(bǔ)充)
這一節(jié)接著寫, 包括數(shù)據(jù)的查詢(補(bǔ)充), 數(shù)據(jù)的更新, 數(shù)據(jù)的刪除
條件查詢
其實(shí)就是根據(jù)給定的一些條件, 然后過濾掉不符合實(shí)際情況的記錄, 把符合條件的記錄返還給用戶, 執(zhí)行這些操作是通過一些運(yùn)算符, 比如說比較運(yùn)算符, 邏輯運(yùn)算符, 和Java中的思路的是一樣的
運(yùn)算符 | 說明 |
---|---|
> , >= , < , <= | 大于, 大于等于, 小于, 小于等于 |
= | 等于, NULL不安全, 如NULL= NULL的結(jié)果就是NULL(參加運(yùn)算) |
<=> | 等于, NULL安全, NULL <=> NULL 的結(jié)果是1(表示真) |
!=, <> | 不等于, NULL不安全 |
betweed m and n | 范圍匹配, [m, n], 如果m <= value <= n, 返回1(表示真) |
小練習(xí)(關(guān)于NULL) :
-- 進(jìn)行簡單的select操作不需要使用數(shù)據(jù)庫
select NULL > 1;
select NULL = NULL;
select NULL <=> NULL;
select not (NULL = NULL);
select not (NULL <=> NULL);
select NULL <> NULL;-- 執(zhí)行結(jié)果 : NULL, NULL, 1, NULL, 0, NULL
-- 總結(jié)就是 :
關(guān)于NULL的數(shù)學(xué)運(yùn)算結(jié)果都是NULL
關(guān)于NULL的含有安全等于的運(yùn)算是有一定意義的
運(yùn)算符 | 說明 |
---|---|
in (option,…) | 如果是option中的任意一個(gè)返回1(表示真) |
is null | 是NULL |
is not null | 不是NULL |
like | 模糊查詢, %表示任意個(gè)(包括0個(gè))任意字符; _表示任意一個(gè)字符 |
小練習(xí) :
-- 進(jìn)行簡單的select操作不需要使用數(shù)據(jù)庫
select 1 in (1, 2, 3);
select 5 in (1, 2, 3);
select NULL is null;
select NULL is not null;
-- 查詢結(jié)果是 1, 0, 1, 0-- 關(guān)于模糊查詢的操作就需要使用數(shù)據(jù)庫了
use sel_test;
select * from exam where name like '%孫%';
-- 上面的查找的是名字中有 '孫' 這個(gè)字的信息
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 6 | 孫權(quán) | 70.0 | 73.0 | 78.5 |
+------+-----------+---------+------+---------+
select * from exam where name like '孫_';
+------+--------+---------+------+---------+
| id | name | chinese | math | english |
+------+--------+---------+------+---------+
| 6 | 孫權(quán) | 70.0 | 73.0 | 78.5 |
+------+--------+---------+------+---------+-- 之前我們查看當(dāng)前數(shù)據(jù)庫的字符集的時(shí)候用的下面這個(gè)指令就是模糊查詢(下面一致)
show variables like '%character%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
show variables like '%charac%';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8mb3 |
| character_sets_dir | C:\Program Files\MySQL\MySQL Server 8.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
運(yùn)算符 | 說明 |
---|---|
and(&&) | 多個(gè)條件必須都為true(1), 結(jié)果才是true(1) |
or(l l) | 任意一個(gè)條件為true(1), 結(jié)果就是true(1) |
not | 條件為true(1), 結(jié)果為false(0), 類似于Java中! |
特殊說明一下, 邏輯運(yùn)算符的優(yōu)先級不建議記憶, 建議使用的時(shí)候還是通過小括號的方式, 手動(dòng)的去指定優(yōu)先級, 使用where條件的語法 |
select * from 表名 where 列名/表達(dá)式 運(yùn)算符 條件;
首先展示一下我們的數(shù)據(jù)中的數(shù)據(jù)(等會(huì)測試驗(yàn)證)
select * from exam;
+------+--------------+---------+------+---------+
| id | name | chinese | math | english |
+------+--------------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.0 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權(quán) | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| 8 | 齊天大圣 | 87.5 | 78.0 | 77.0 |
| 9 | 孫行者 | NULL | 84.0 | 83.5 |
| 10 | 宋江 | 76.0 | NULL | 77.0 |
| 11 | 李逵 | 67.0 | 90.0 | NULL |
+------+--------------+---------+------+---------+
練習(xí)
需求1 : 找到英語成績不及格的同學(xué)(english < 60)
mysql> select * from exam where english < 60;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
+------+-----------+---------+------+---------+
因?yàn)?NULL < 60 不論什么時(shí)候都是不成立的, 所以直接過濾, 留下三條記錄
需求2 : 找打數(shù)學(xué)成績好于英語成績的同學(xué)(math > english)
mysql> select * from exam where math > english;
+------+--------------+---------+------+---------+
| id | name | chinese | math | english |
+------+--------------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.0 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| 8 | 齊天大圣 | 87.5 | 78.0 | 77.0 |
| 9 | 孫行者 | NULL | 84.0 | 83.5 |
+------+--------------+---------+------+---------+
需求三 : 查詢總分在200以下的同學(xué)(結(jié)果降序排列)
select id, name, chinese + math + english
as '總分' from exam where chinese + math + english < 200
order by '總分' desc;
+------+-----------+--------+
| id | name | 總分 |
+------+-----------+--------+
| 5 | 劉玄德 | 185.5 |
| 7 | 宋公明 | 170.0 |
+------+-----------+--------+
思考上面的代碼, 如果把where中的chinese + math + english替換為’總分’可以么 ?
關(guān)于SQL語句的執(zhí)行順序
主要就是下面幾個(gè)點(diǎn)的執(zhí)行順序
- select 2. from 3. where 4. order by
關(guān)于這個(gè)問題, 我有一套自己的邏輯, 我們可以把一個(gè)數(shù)據(jù)庫類比為一個(gè)數(shù)據(jù)池, 我們想要從中篩選指定的數(shù)據(jù), 首先要搬出來數(shù)據(jù)池, 也就是from, 那么就需要一個(gè)濾網(wǎng)去過濾, 也就是where, 篩選出來指定的數(shù)據(jù)之后就select把數(shù)據(jù)選到結(jié)果集, 最后再次進(jìn)行排序(order by)
也就是首先是from, 然后是where, 其次是select, 最后是order by
where中的別名問題
由上面的結(jié)論可以得到, 如果在where中通過別名進(jìn)行篩選的話, 就不會(huì)篩選出來指定的結(jié)果, 但是有一個(gè)例外, 就是當(dāng)別名通過引號進(jìn)行標(biāo)注的話, 就可以正常的執(zhí)行
但是也會(huì)生成一個(gè)警告
分頁查詢(LIMIT)
限制結(jié)果集中的數(shù)據(jù)的條數(shù), 在上一節(jié)我們說過如果不對數(shù)據(jù)的條數(shù)進(jìn)行限制, 是一個(gè)很危險(xiǎn)的操作, 所以就出現(xiàn)了分頁查詢的操作, 分頁查詢在實(shí)際的項(xiàng)目中應(yīng)用是十分的頻繁的, 只要是多條數(shù)據(jù)的集合, 一般都是用分頁進(jìn)行查詢, 分頁查詢的優(yōu)點(diǎn)也是很明顯的, 可以有效的控制一次查詢出來的結(jié)果集記錄的條數(shù), 減小數(shù)據(jù)庫的壓力, 同時(shí)對用戶也是十分友好的
基礎(chǔ)語法
-- 第一種分頁語法(從索引為0的位置開始查詢n條記錄, 索引的起始位置是0)
select[字段]from[表名]where[查詢條件]order by[字段][asc|desc]limit n;
-- 第二種分頁語法(從索引為s的位置開始查詢n條記錄)
select[字段]from[表名]where[查詢條件]order by[字段][asc|desc]limit s,n;
-- 第三種分頁語法(跟第二種一樣但是語法更加嚴(yán)密)
select[字段]from[表名]where[查詢條件]order by[字段][asc|desc]limit n offset s;
假設(shè)上面的數(shù)據(jù)庫我們要用分頁查詢法, 每4個(gè)是一頁, 按照id排序
-- 分頁的偏移量是 (頁數(shù) - 1) * 每頁數(shù)據(jù)量
mysql> select * from exam order by id limit 4 offset 0;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 1 | 唐三藏 | 67.0 | 98.0 | 56.0 |
| 2 | 孫悟空 | 87.5 | 78.0 | 77.0 |
| 3 | 豬悟能 | 88.0 | 98.0 | 90.0 |
| 4 | 曹孟德 | 82.0 | 84.0 | 67.0 |
+------+-----------+---------+------+---------+
4 rows in set (0.00 sec)mysql> select * from exam order by id limit 4 offset 4;
+------+--------------+---------+------+---------+
| id | name | chinese | math | english |
+------+--------------+---------+------+---------+
| 5 | 劉玄德 | 55.5 | 85.0 | 45.0 |
| 6 | 孫權(quán) | 70.0 | 73.0 | 78.5 |
| 7 | 宋公明 | 75.0 | 65.0 | 30.0 |
| 8 | 齊天大圣 | 87.5 | 78.0 | 77.0 |
+------+--------------+---------+------+---------+
4 rows in set (0.00 sec)mysql> select * from exam order by id limit 4 offset 8;
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 9 | 孫行者 | NULL | 84.0 | 83.5 |
| 10 | 宋江 | 76.0 | NULL | 77.0 |
| 11 | 李逵 | 67.0 | 90.0 | NULL |
+------+-----------+---------+------+---------+
3 rows in set (0.00 sec)
現(xiàn)在分頁查詢需求是按照總分進(jìn)行排序(每一頁四條數(shù)據(jù))
mysql> select id, name, chinese + math + english as '總分' from exam order by '總分' desc limit 4 offset 0;
+------+-----------+--------+
| id | name | 總分 |
+------+-----------+--------+
| 1 | 唐三藏 | 221.0 |
| 2 | 孫悟空 | 242.5 |
| 3 | 豬悟能 | 276.0 |
| 4 | 曹孟德 | 233.0 |
+------+-----------+--------+
4 rows in set (0.00 sec)mysql> select id, name, chinese + math + english as '總分' from exam order by '總分' desc limit 4 offset 4;
+------+--------------+--------+
| id | name | 總分 |
+------+--------------+--------+
| 5 | 劉玄德 | 185.5 |
| 6 | 孫權(quán) | 221.5 |
| 7 | 宋公明 | 170.0 |
| 8 | 齊天大圣 | 242.5 |
+------+--------------+--------+
4 rows in set (0.00 sec)mysql> select id, name, chinese + math + english as '總分' from exam order by '總分' desc limit 4 offset 8;
+------+-----------+--------+
| id | name | 總分 |
+------+-----------+--------+
| 9 | 孫行者 | NULL |
| 10 | 宋江 | NULL |
| 11 | 李逵 | NULL |
+------+-----------+--------+
3 rows in set (0.00 sec)
思考, 我們的null進(jìn)行運(yùn)算完之后數(shù)據(jù)都是null, 但現(xiàn)在我們的需求是如果是null默認(rèn)為0, 那就要用到我們ifnull函數(shù)了, 關(guān)于函數(shù)我們會(huì)詳細(xì)講解, 這里簡單說一下
select id, name, ifnull(chinese, 0) + ifnull(math, 0) + ifnull(english, 0) as '總分' from exam order by '總分' desc limit 4 offset 0;select id, name, ifnull(chinese, 0) + ifnull(math, 0) + ifnull(english, 0) as '總分' from exam order by '總分' desc limit 4 offset 4;select id, name, ifnull(chinese, 0) + ifnull(math, 0) + ifnull(english, 0) as '總分' from exam order by '總分' desc limit 4 offset 8;
最后一個(gè)SQL語句執(zhí)行結(jié)果就會(huì)按照需求進(jìn)行調(diào)整
+------+-----------+--------+
| id | name | 總分 |
+------+-----------+--------+
| 9 | 孫行者 | 167.5 |
| 10 | 宋江 | 153.0 |
| 11 | 李逵 | 157.0 |
+------+-----------+--------+
數(shù)據(jù)的修改
數(shù)據(jù)修改基礎(chǔ)知識
也就是CRUD中的U(update), 更新某一張表中的數(shù)據(jù), 這個(gè)才是真正意義上的’更改器’
基礎(chǔ)語法
update [表名] set [字段1] = [期望值] ... where 篩選條件 order by 排序規(guī)則 limit..
需求1 : 將孫悟空同學(xué)的數(shù)學(xué)成績變?yōu)?0分
Rows matched 指的是匹配到了多少行, Changed 是指真正修改的數(shù)據(jù)行數(shù)
mysql> select * from exam where name = '孫悟空';
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 2 | 孫悟空 | 87.5 | 80.0 | 77.0 |
+------+-----------+---------+------+---------+
需求2 : 將孫悟空同學(xué)的語文成績加20分
注意這里不可以使用math += 10, mysql不支持這種操作
mysql> select * from exam where name = '孫悟空';
+------+-----------+---------+------+---------+
| id | name | chinese | math | english |
+------+-----------+---------+------+---------+
| 2 | 孫悟空 | 97.5 | 80.0 | 77.0 |
+------+-----------+---------+------+---------+
需求3 : 把總分排名倒數(shù)前3的人的數(shù)學(xué)成績加上10分
update exam set math = math + 10 where math is not null order by math + chinese + english asc limit 3;