最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文掌握MySQL基本查詢操作

 更新時(shí)間:2026年01月23日 11:53:28   作者:阿拉伯檸檬  
文章介紹了MySQL數(shù)據(jù)庫(kù)中表的增刪改查操作,包括Create(創(chuàng)建)、Retrieve(讀?。?、Update(更新)和Delete(刪除),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

前言

表的增刪改查:
CRUD : Create(創(chuàng)建), Retrieve(讀取),Update(更新),Delete(刪除)

一、Create

語(yǔ)法:

INSERT [INTO] table_name
[(column [, column] ...)]
VALUES (value_list) [, (value_list)] ...
value_list: value, [, value] ...

創(chuàng)建一張表:

mysql> create table students(
    -> id int unsigned primary key auto_increment,
    -> sn int not null unique key comment "學(xué)號(hào)",
    ->  name varchar(20) not null,
    -> qq varchar(32) unique key
    -> );
Query OK, 0 rows affected (0.02 sec)
  1. 單行數(shù)據(jù) + 全列插入

value_list 數(shù)量必須和定義表的列的數(shù)量及順序一致

mysql> insert into students values (66, 123, '劉備', 111111);
Query OK, 1 row affected (0.00 sec)
  1. 多行數(shù)據(jù) + 指定列插入

這里可以靈活插入

mysql> insert into students (sn, name) values (124, '關(guān)羽');
Query OK, 1 row affected (0.00 sec)
mysql> insert into students (name, sn) values ('張飛', 125), ('曹操', 126), ('貂蟬', 128);
Query OK, 1 row affected (0.00 sec)
mysql> select * from students;
+----+-----+--------+--------+
| id | sn  | name   | qq     |
+----+-----+--------+--------+
| 66 | 123 | 劉備   | 111111 |
| 67 | 124 | 關(guān)羽   | NULL   |
| 68 | 125 | 張飛   | NULL   |
| 69 | 126 | 曹操   | NULL   |
| 70 | 128 | 貂蟬   | NULL   |
+----+-----+--------+--------+
5 rows in set (0.00 sec)
  1. 插入否則更新

由于 主鍵 或者 唯一鍵 對(duì)應(yīng)的值已經(jīng)存在而導(dǎo)致插入失?。?/p>

mysql> insert into students values (70, 129, '西施', 22);
ERROR 1062 (23000): Duplicate entry '70' for key 'students.PRIMARY'
mysql> insert into students values (71, 129, '西施', 111111);
ERROR 1062 (23000): Duplicate entry '70' for key 'students.PRIMARY'

可以選擇性的進(jìn)行同步更新操作
語(yǔ)法:

INSERT ... ON DUPLICATE KEY UPDATE
	column = value [, column = value] ...
mysql> insert into students values (71 ,129, '西施', 111111) on duplicate key update id = 71, name = '西施', qq = 222222;
Query OK, 2 rows affected (0.00 sec)
mysql> select * from students;
+----+-----+--------+--------+
| id | sn  | name   | qq     |
+----+-----+--------+--------+
| 67 | 124 | 關(guān)羽   | NULL   |
| 68 | 125 | 張飛   | NULL   |
| 69 | 126 | 曹操   | NULL   |
| 70 | 128 | 貂蟬   | NULL   |
| 71 | 123 | 西施   | 222222 |
+----+-----+--------+--------+
5 rows in set (0.00 sec)
  1. 替換

主鍵 或者 唯一鍵 沒(méi)有沖突,則直接插入;
主鍵 或者 唯一鍵 如果沖突,則刪除后再插入

mysql> replace into students (sn, name, qq) values (130, '呂布', 333333);
Query OK, 1 row affected (0.00 sec)
mysql> replace into students (sn, name, qq) values (130, '呂布2', 333333);
Query OK, 2 rows affected (0.00 sec)
-- 1 row affected: 表中沒(méi)有沖突數(shù)據(jù),數(shù)據(jù)被插入
-- 2 row affected: 表中有沖突數(shù)據(jù),刪除后重新插入
mysql> select * from students;
+----+-----+---------+--------+
| id | sn  | name    | qq     |
+----+-----+---------+--------+
| 67 | 124 | 關(guān)羽    | NULL   |
| 68 | 125 | 張飛    | NULL   |
| 69 | 126 | 曹操    | NULL   |
| 70 | 128 | 貂蟬    | NULL   |
| 71 | 123 | 西施    | 222222 |
| 73 | 130 | 呂布2   | 333333 |
+----+-----+---------+--------+
6 rows in set (0.00 sec)

二、Retrieve

新建學(xué)生成績(jī)表:

mysql> create table exam_result (
    -> id int unsigned primary key auto_increment,
    -> name varchar(20) not null comment '姓名',
    -> chinese float default 0.0 comment '語(yǔ)文成績(jī)',
    -> math float default 0.0 comment '數(shù)學(xué)成績(jī)',
    -> english float default 0.0 comment '英語(yǔ)成績(jī)'
    -> );
Query OK, 0 rows affected (0.01 sec)
mysql> insert into exam_result (name, chinese, math, english) values
    -> ('唐三藏', 67, 98, 56),
    -> ('孫悟空', 87, 78, 77),
    -> ('豬悟能', 88, 98, 90),
    -> ('曹孟德', 82, 84, 67),
    -> ('劉玄德', 55, 85, 45),
    -> ('孫權(quán)', 70, 73, 78),
    -> ('宋公明', 75, 65, 30);
Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

select 列

  • 全列查詢

通常情況下不建議全列查詢,一方面是傳輸?shù)臄?shù)據(jù)量可能非常大,而且可能會(huì)影響到索引的使用。

mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孫悟空    |      87 |   78 |      77 |
|  3 | 豬悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 劉玄德    |      55 |   85 |      45 |
|  6 | 孫權(quán)      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
  • 指定列查詢

指定列的順序可以自由選擇

mysql> select name, chinese, id from exam_result;
+-----------+---------+----+
| name      | chinese | id |
+-----------+---------+----+
| 唐三藏    |      67 |  1 |
| 孫悟空    |      87 |  2 |
| 豬悟能    |      88 |  3 |
| 曹孟德    |      82 |  4 |
| 劉玄德    |      55 |  5 |
| 孫權(quán)      |      70 |  6 |
| 宋公明    |      75 |  7 |
+-----------+---------+----+
7 rows in set (0.00 sec)
  • 查詢字段為表達(dá)式

表達(dá)式不包含字段

mysql> select id, name, 10 from exam_result;
+----+-----------+----+
| id | name      | 10 |
+----+-----------+----+
|  1 | 唐三藏    | 10 |
|  2 | 孫悟空    | 10 |
|  3 | 豬悟能    | 10 |
|  4 | 曹孟德    | 10 |
|  5 | 劉玄德    | 10 |
|  6 | 孫權(quán)      | 10 |
|  7 | 宋公明    | 10 |
+----+-----------+----+
7 rows in set (0.00 sec)

表達(dá)式包含一個(gè)字段

mysql> select id, name, chinese + 100 from exam_result;
+----+-----------+---------------+
| id | name      | chinese + 100 |
+----+-----------+---------------+
|  1 | 唐三藏    |           167 |
|  2 | 孫悟空    |           187 |
|  3 | 豬悟能    |           188 |
|  4 | 曹孟德    |           182 |
|  5 | 劉玄德    |           155 |
|  6 | 孫權(quán)      |           170 |
|  7 | 宋公明    |           175 |
+----+-----------+---------------+
7 rows in set (0.00 sec)

表達(dá)式包含多個(gè)字段

mysql> select id, name, chinese + math + english from exam_result;
+----+-----------+--------------------------+
| id | name      | chinese + math + english |
+----+-----------+--------------------------+
|  1 | 唐三藏    |                      221 |
|  2 | 孫悟空    |                      242 |
|  3 | 豬悟能    |                      276 |
|  4 | 曹孟德    |                      233 |
|  5 | 劉玄德    |                      185 |
|  6 | 孫權(quán)      |                      221 |
|  7 | 宋公明    |                      170 |
+----+-----------+--------------------------+
7 rows in set (0.00 sec)
  • 為查詢結(jié)果指定別名

語(yǔ)法:

SELECT column [AS] alias_name [...] FROM table_name;
mysql> select id, name, chinese + math + english 總分 from exam_result;
+----+-----------+--------+
| id | name      | 總分   |
+----+-----------+--------+
|  1 | 唐三藏    |    221 |
|  2 | 孫悟空    |    242 |
|  3 | 豬悟能    |    276 |
|  4 | 曹孟德    |    233 |
|  5 | 劉玄德    |    185 |
|  6 | 孫權(quán)      |    221 |
|  7 | 宋公明    |    170 |
+----+-----------+--------+
7 rows in set (0.00 sec)
  • 結(jié)果去重 distinct

distinct 必須緊跟在 select 之后,并且作用于所有選擇的列。

數(shù)學(xué)成績(jī)98分重復(fù)了:

mysql> select math from exam_result;
+------+
| math |
+------+
|   98 |
|   78 |
|   98 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+
7 rows in set (0.00 sec)

去重:

mysql> select distinct math from exam_result;
+------+
| math |
+------+
|   98 |
|   78 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+
6 rows in set (0.00 sec)

where條件

比較運(yùn)算符:

邏輯運(yùn)算符:

運(yùn)算符優(yōu)先級(jí)(從高到低):

  • 英語(yǔ)不及格的同學(xué)及英語(yǔ)成績(jī) ( < 60 )
mysql> select name, english from exam_result where english < 60;
+-----------+---------+
| name      | english |
+-----------+---------+
| 唐三藏    |      56 |
| 劉玄德    |      45 |
| 宋公明    |      30 |
+-----------+---------+
3 rows in set (0.00 sec)
  • 語(yǔ)文成績(jī)?cè)?[80, 90] 分的同學(xué)及語(yǔ)文成績(jī)
--使用and連接
mysql> select name, chinese from exam_result where chinese >= 80 and chinese <= 90;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 孫悟空    |      87 |
| 豬悟能    |      88 |
| 曹孟德    |      82 |
+-----------+---------+
3 rows in set (0.00 sec)
--使用between and 連接
mysql> select name, chinese from exam_result where chinese between 80 and 90;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 孫悟空    |      87 |
| 豬悟能    |      88 |
| 曹孟德    |      82 |
+-----------+---------+
3 rows in set (0.00 sec)
  • 數(shù)學(xué)成績(jī)是 58 或者 59 或者 98 或者 99 分的同學(xué)及數(shù)學(xué)成績(jī)
--使用or連接
mysql> select name, math from exam_result where math = 58 or math = 59 or math = 98 or math = 99;
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 豬悟能    |   98 |
+-----------+------+
2 rows in set (0.00 sec)
--使用in連接
mysql> select name, math from exam_result where math in (58, 59, 98, 99);
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 豬悟能    |   98 |
+-----------+------+
2 rows in set (0.00 sec)
  • 姓孫的同學(xué) 及 孫某同學(xué)
--姓孫的同學(xué)
mysql> select name from exam_result where name like ('孫%');
+-----------+
| name      |
+-----------+
| 孫悟空    |
| 孫權(quán)      |
+-----------+
2 rows in set (0.00 sec)
--孫某同學(xué)
mysql> select name from exam_result where name like ('孫_');
+--------+
| name   |
+--------+
| 孫權(quán)   |
+--------+
1 row in set (0.00 sec)
  • 語(yǔ)文成績(jī)好于英語(yǔ)成績(jī)的同學(xué)
mysql> SELECT name, chinese, english FROM exam_result WHERE chinese > english;
+-----------+---------+---------+
| name      | chinese | english |
+-----------+---------+---------+
| 唐三藏    |      67 |      56 |
| 孫悟空    |      87 |      77 |
| 曹孟德    |      82 |      67 |
| 劉玄德    |      55 |      45 |
| 宋公明    |      75 |      30 |
+-----------+---------+---------+
5 rows in set (0.00 sec)
  • 總分在 200 分以下的同學(xué)

別名不能用在 where 條件中

mysql> select name, chinese + math +english 總分 from exam_result where chinese + math + english < 200;
+-----------+--------+
| name      | 總分   |
+-----------+--------+
| 劉玄德    |    185 |
| 宋公明    |    170 |
+-----------+--------+
2 rows in set (0.00 sec)
  • 語(yǔ)文成績(jī) > 80 并且不姓孫的同學(xué)
mysql> select name, chinese from exam_result where chinese > 80 and name not like ('孫%');
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 豬悟能    |      88 |
| 曹孟德    |      82 |
+-----------+---------+
2 rows in set (0.00 sec)
  • 孫姓同學(xué),否則要求總成績(jī) > 200 并且 語(yǔ)文成績(jī) < 數(shù)學(xué)成績(jī) 并且 英語(yǔ)成績(jī) > 80
mysql> select name, chinese, math, english, chinese + math + english total from exam_result where name like ('孫%') or (chinese + math + english > 200 and chinese < math and english > 80);
+-----------+---------+------+---------+-------+
| name      | chinese | math | english | total |
+-----------+---------+------+---------+-------+
| 孫悟空    |      87 |   78 |      77 |   242 |
| 豬悟能    |      88 |   98 |      90 |   276 |
| 孫權(quán)      |      70 |   73 |      78 |   221 |
+-----------+---------+------+---------+-------+
3 rows in set (0.00 sec)

結(jié)果排序 order by

語(yǔ)法:
– ASC 為升序(從小到大)
– DESC 為降序(從大到?。?br />– 默認(rèn)為 ASC

SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];

注意:在MySQL中,沒(méi)有 order by 子句的查詢,返回的順序是未定義的,永遠(yuǎn)不要依賴這個(gè)順序

  • 同學(xué)及數(shù)學(xué)成績(jī),按數(shù)學(xué)成績(jī)升序顯示
mysql> select name, math from exam_result order by math;
+-----------+------+
| name      | math |
+-----------+------+
| 宋公明    |   65 |
| 孫權(quán)      |   73 |
| 孫悟空    |   78 |
| 曹孟德    |   84 |
| 劉玄德    |   85 |
| 唐三藏    |   98 |
| 豬悟能    |   98 |
+-----------+------+
7 rows in set (0.00 sec)
  • 查詢同學(xué)各門成績(jī),依次按 數(shù)學(xué)降序,英語(yǔ)升序,語(yǔ)文升序的方式顯示

排序規(guī)則:
先按照第一個(gè)字段(math)排序,當(dāng)?shù)谝粋€(gè)字段的值相同時(shí),再按照第二個(gè)字段(english)排序,如果第二個(gè)字段也相同,則按照第三個(gè)字段(chinese)排序。

mysql> select name, math, english, chinese from exam_result order by math desc, english, chinese;
+-----------+------+---------+---------+
| name      | math | english | chinese |
+-----------+------+---------+---------+
| 唐三藏    |   98 |      56 |      67 |
| 豬悟能    |   98 |      90 |      88 |
| 劉玄德    |   85 |      45 |      55 |
| 曹孟德    |   84 |      67 |      82 |
| 孫悟空    |   78 |      77 |      87 |
| 孫權(quán)      |   73 |      78 |      70 |
| 宋公明    |   65 |      30 |      75 |
+-----------+------+---------+---------+
7 rows in set (0.00 sec)
  • 查詢同學(xué)及總分,由高到低
mysql> select name, chinese + math + english total from exam_result order by chinese + math + english desc;
+-----------+-------+
| name      | total |
+-----------+-------+
| 豬悟能    |   276 |
| 孫悟空    |   242 |
| 曹孟德    |   233 |
| 唐三藏    |   221 |
| 孫權(quán)      |   221 |
| 劉玄德    |   185 |
| 宋公明    |   170 |
+-----------+-------+
7 rows in set (0.00 sec)
--order by 字句中可以使用列別名
mysql> select name, chinese + math + english total from exam_result order by total desc;
+-----------+-------+
| name      | total |
+-----------+-------+
| 豬悟能    |   276 |
| 孫悟空    |   242 |
| 曹孟德    |   233 |
| 唐三藏    |   221 |
| 孫權(quán)      |   221 |
| 劉玄德    |   185 |
| 宋公明    |   170 |
+-----------+-------+
7 rows in set (0.00 sec)
  • 查詢姓孫的同學(xué)或者姓曹的同學(xué)數(shù)學(xué)成績(jī),結(jié)果按數(shù)學(xué)成績(jī)由高到低顯示
mysql> select name, math from exam_result where name like ('孫%') or name like ('曹%') order by math desc;
+-----------+------+
| name      | math |
+-----------+------+
| 曹孟德    |   84 |
| 孫悟空    |   78 |
| 孫權(quán)      |   73 |
+-----------+------+
3 rows in set (0.00 sec)

篩選分頁(yè)結(jié)果 limit

語(yǔ)法:
– 起始下標(biāo)為 0
– 從 0 開(kāi)始,篩選 n 條結(jié)果
SELECT … FROM table_name [WHERE …] [ORDER BY …] LIMIT n;

– 從 s 開(kāi)始,篩選 n 條結(jié)果
SELECT … FROM table_name [WHERE …] [ORDER BY …] LIMIT s, n;

– 從 s 開(kāi)始,篩選 n 條結(jié)果,比第二種用法更明確,建議使用
SELECT … FROM table_name [WHERE …] [ORDER BY …] LIMIT n OFFSET s;

注意:
對(duì)未知表進(jìn)行查詢時(shí),最好加一條 LIMIT 1,避免因?yàn)楸碇袛?shù)據(jù)過(guò)大,查詢?nèi)頂?shù)據(jù)導(dǎo)致數(shù)據(jù)庫(kù)卡死

  • 按 id 進(jìn)行分頁(yè),每頁(yè) 3 條記錄,分別顯示 第 1、2、3 頁(yè)
--第1頁(yè)
mysql> select * from exam_result order by id limit 0, 3;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孫悟空    |      87 |   78 |      77 |
|  3 | 豬悟能    |      88 |   98 |      90 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)
--第2頁(yè)
mysql> select * from exam_result order by id limit 3, 3;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 劉玄德    |      55 |   85 |      45 |
|  6 | 孫權(quán)      |      70 |   73 |      78 |
+----+-----------+---------+------+---------+
3 rows in set (0.00 sec)
--第3頁(yè)
mysql> select * from exam_result order by id limit 6, 3;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)

三、Update

語(yǔ)法:
對(duì)查詢到的結(jié)果進(jìn)行列值更新:

UPDATE table_name SET column = expr [, column = expr ...]
	[WHERE ...] [ORDER BY ...] [LIMIT ...]
  • 將孫悟空同學(xué)的數(shù)學(xué)成績(jī)變更為 80 分
-- 查看原數(shù)據(jù)
SELECT name, math FROM exam_result WHERE name = '孫悟空';
+-----------+--------+
| name | math |
+-----------+--------+
| 孫悟空 | 78 |
+-----------+--------+
1 row in set (0.00 sec)
-- 數(shù)據(jù)更新
UPDATE exam_result SET math = 80 WHERE name = '孫悟空';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-- 查看更新后數(shù)據(jù)
SELECT name, math FROM exam_result WHERE name = '孫悟空';
+-----------+--------+
| name | math |
+-----------+--------+
| 孫悟空 | 80 |
+-----------+--------+
1 row in set (0.00 sec)
  • 將曹孟德同學(xué)的數(shù)學(xué)成績(jī)變更為 60 分,語(yǔ)文成績(jī)變更為 70 分
-- 查看原數(shù)據(jù)
SELECT name, math, chinese FROM exam_result WHERE name = '曹孟德';
+-----------+--------+-------+
e | math | chinese |
+-----------+--------+-------+
| 曹孟德 | 84 | 82 |
+-----------+--------+-------+
1 row in set (0.00 sec)
-- 數(shù)據(jù)更新
UPDATE exam_result SET math = 60, chinese = 70 WHERE name = '曹孟德';
Query OK, 1 row affected (0.14 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-- 查看更新后數(shù)據(jù)
SELECT name, math, chinese FROM exam_result WHERE name = '曹孟德';
+-----------+--------+-------+
| name | math | chinese |
+-----------+--------+-------+
| 曹孟德 | 60 | 70 |
+-----------+--------+-------+
1 row in set (0.00 sec)
  • 將總成績(jī)倒數(shù)前三的 3 位同學(xué)的數(shù)學(xué)成績(jī)加上 30 分
-- 查看原數(shù)據(jù)
-- 別名可以在ORDER BY中使用
SELECT name, math, chinese + math + english 總分 FROM exam_result ORDER BY 總分 LIMIT 3;
+-----------+--------+--------+
| name | math | 總分 |
+-----------+--------+--------+
| 宋公明 | 65 | 170 |
| 劉玄德 | 85 | 185 |
| 曹孟德 | 60 | 197 |
+-----------+--------+--------+
3 rows in set (0.00 sec)
-- 數(shù)據(jù)更新,mysql不支持 math += 30 這種語(yǔ)法
UPDATE exam_result SET math = math + 30 ORDER BY chinese + math + english LIMIT 3;
-- 查看更新后數(shù)據(jù)
--這里還可以按總分升序排序取前 3 個(gè)
SELECT name, math, chinese + math + english 總分 FROM exam_result WHERE name IN ('宋公明', '劉玄德', '曹孟德');
+-----------+--------+--------+
| name | math | 總分 |
+-----------+--------+--------+
| 曹孟德 | 90 | 227 |
| 劉玄德 | 115 | 215 |
| 宋公明 | 95 | 200 |
+-----------+--------+--------+
3 rows in set (0.00 sec)
-- 按總成績(jī)排序后查詢結(jié)果
SELECT name, math, chinese + math + english 總分 FROM exam_result ORDER BY 總分 LIMIT 3;
+-----------+--------+--------+
| name | math | 總分 |
+-----------+--------+--------+
| 宋公明 | 95 | 200 |
| 劉玄德 | 115 | 215 |
| 唐三藏 | 98 | 221 |
+-----------+--------+--------+
3 rows in set (0.00 sec)
  • 將所有同學(xué)的語(yǔ)文成績(jī)更新為原來(lái)的 2 倍
-- 查看原數(shù)據(jù)
SELECT * FROM exam_result;
+----+-----------+-------+--------+--------+
| id | name | chinese | math | english |
+----+-----------+-------+--------+--------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孫悟空 | 87 | 80 | 77 |
| 3 | 豬悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 70 | 90 | 67 |
| 5 | 劉玄德 | 55 | 115 | 45 |
| 6 | 孫權(quán) | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 95 | 30 |
+----+-----------+-------+--------+--------+
7 rows in set (0.00 sec)
-- 數(shù)據(jù)更新
UPDATE exam_result SET chinese = chinese * 2;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7 Changed: 7 Warnings: 0
-- 查看更新后數(shù)據(jù)
SELECT * FROM exam_result;
+----+-----------+-------+--------+--------+
| id | name | chinese | math | english |
+----+-----------+-------+--------+--------+
| 1 | 唐三藏 | 134 | 98 | 56 |
| 2 | 孫悟空 | 174 | 80 | 77 |
| 3 | 豬悟能 | 176 | 98 | 90 |
| 4 | 曹孟德 | 140 | 90 | 67 |
| 5 | 劉玄德 | 110 | 115 | 45 |
| 6 | 孫權(quán) | 140 | 73 | 78 |
| 7 | 宋公明 | 150 | 95 | 30 |
+----+-----------+-------+--------+--------+
7 rows in set (0.00 sec)

四、Delete

刪除數(shù)據(jù)

語(yǔ)法:

DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
  • 刪除孫悟空同學(xué)的考試成績(jī)
-- 查看原數(shù)據(jù)
SELECT * FROM exam_result WHERE name = '孫悟空';
+----+-----------+-------+--------+--------+
| id | name | chinese | math | english |
+----+-----------+-------+--------+--------+
| 2 | 孫悟空 | 174 | 80 | 77 |
+----+-----------+-------+--------+--------+
1 row in set (0.00 sec)
-- 刪除數(shù)據(jù)
DELETE FROM exam_result WHERE name = '孫悟空';
Query OK, 1 row affected (0.17 sec)
-- 查看刪除結(jié)果
SELECT * FROM exam_result WHERE name = '孫悟空';
Empty set (0.00 sec)
  • 刪除整張表數(shù)據(jù)
-- 準(zhǔn)備測(cè)試表
CREATE TABLE for_delete (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20)
);
Query OK, 0 rows affected (0.16 sec)
-- 插入測(cè)試數(shù)據(jù)
INSERT INTO for_delete (name) VALUES ('A'), ('B'), ('C');
Query OK, 3 rows affected (1.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
-- 查看測(cè)試數(shù)據(jù)
SELECT * FROM for_delete;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
3 rows in set (0.00 sec)
-- 刪除整表數(shù)據(jù)
DELETE FROM for_delete;
Query OK, 3 rows affected (0.00 sec)
-- 查看刪除結(jié)果
SELECT * FROM for_delete;
Empty set (0.00 sec)
-- 再插入一條數(shù)據(jù),自增 id 在原值上增長(zhǎng)
INSERT INTO for_delete (name) VALUES ('D');
Query OK, 1 row affected (0.00 sec)
-- 查看數(shù)據(jù)
SELECT * FROM for_delete;
+----+------+
| id | name |
+----+------+
| 4 | D |
+----+------+
1 row in set (0.00 sec)
-- 查看表結(jié)構(gòu),會(huì)有 AUTO_INCREMENT=n 項(xiàng)
SHOW CREATE TABLE for_delete\G
*************************** 1. row ***************************
Table: for_delete
Create Table: CREATE TABLE `for_delete` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

截?cái)啾恚?/h3>

語(yǔ)法:

TRUNCATE [TABLE] table_name

注意:
這個(gè)操作慎重使用

  1. 只能對(duì)整表操作,不能像 DELETE 一樣針對(duì)部分?jǐn)?shù)據(jù)操作;
  2. 實(shí)際上 MySQL 不對(duì)數(shù)據(jù)操作,所以比 DELETE 更快,但是TRUNCATE在刪除數(shù)據(jù)的時(shí)候,并不經(jīng)過(guò)真正的事物,所以無(wú)法回滾
  3. 會(huì)重置 AUTO_INCREMENT 項(xiàng)
-- 準(zhǔn)備測(cè)試表
CREATE TABLE for_truncate (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20)
);
Query OK, 0 rows affected (0.16 sec)
-- 插入測(cè)試數(shù)據(jù)
INSERT INTO for_truncate (name) VALUES ('A'), ('B'), ('C');
Query OK, 3 rows affected (1.05 sec)
Records: 3 Duplicates: 0 Warnings: 0
-- 查看測(cè)試數(shù)據(jù)
SELECT * FROM for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
+----+------+
3 rows in set (0.00 sec)
-- 截?cái)嗾頂?shù)據(jù),注意影響行數(shù)是 0,所以實(shí)際上沒(méi)有對(duì)數(shù)據(jù)真正操作
TRUNCATE for_truncate;
Query OK, 0 rows affected (0.10 sec)
-- 查看刪除結(jié)果
SELECT * FROM for_truncate;
Empty set (0.00 sec)
-- 再插入一條數(shù)據(jù),自增 id 在重新增長(zhǎng)
INSERT INTO for_truncate (name) VALUES ('D');
Query OK, 1 row affected (0.00 sec)
-- 查看數(shù)據(jù)
SELECT * FROM for_truncate;
+----+------+
| id | name |
+----+------+
| 1 | D |
+----+------+
1 row in set (0.00 sec)
-- 查看表結(jié)構(gòu),會(huì)有 AUTO_INCREMENT=2 項(xiàng)
SHOW CREATE TABLE for_truncate\G
*************************** 1. row ***************************
Table: for_truncate
Create Table: CREATE TABLE `for_truncate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

插入查詢結(jié)果

語(yǔ)法:

INSERT INTO table_name [(column [, column ...])] SELECT ...

樣例:
刪除表中的的重復(fù)復(fù)記錄,將 duplicate_table 的去重?cái)?shù)據(jù)插入到 no_duplicate_table

-- 創(chuàng)建原數(shù)據(jù)表
CREATE TABLE duplicate_table (id int, name varchar(20));
Query OK, 0 rows affected (0.01 sec)
-- 插入測(cè)試數(shù)據(jù)
INSERT INTO duplicate_table VALUES
(100, 'aaa'),
(100, 'aaa'),
(200, 'bbb'),
(200, 'bbb'),
(200, 'bbb'),
(300, 'ccc');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
-- 創(chuàng)建一張空表 no_duplicate_table,結(jié)構(gòu)和 duplicate_table 一樣
CREATE TABLE no_duplicate_table LIKE duplicate_table;
Query OK, 0 rows affected (0.00 sec)
-- 將 duplicate_table 的去重?cái)?shù)據(jù)插入到 no_duplicate_table
INSERT INTO no_duplicate_table SELECT DISTINCT * FROM duplicate_table;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
-- 通過(guò)重命名表,實(shí)現(xiàn)原子的去重操作
RENAME TABLE duplicate_table TO old_duplicate_table, no_duplicate_table TO duplicate_table;
Query OK, 0 rows affected (0.00 sec)
-- 查看最終結(jié)果
SELECT * FROM duplicate_table;
+------+------+
| id | name |
+------+------+
| 100 | aaa |
| 200 | bbb |
| 300 | ccc |
+------+------+
3 rows in set (0.00 sec)
-- 刪除備份表duplicate_table
drop table dulpicate_table;

到此這篇關(guān)于MySQL基本查詢的文章就介紹到這了,更多相關(guān)MySQL基本查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

定日县| 资中县| 宁晋县| 莱西市| 和林格尔县| 万源市| 吉林市| 永兴县| 玛曲县| 南陵县| 合作市| 木兰县| 寿宁县| 日喀则市| 荔浦县| 连南| 呼和浩特市| 江津市| 阿克| 彰武县| 万盛区| 蒲城县| 手机| 阿拉善左旗| 宜丰县| 新昌县| 车险| 错那县| 岳阳市| 中方县| 化德县| 本溪市| 邵阳市| 远安县| 深州市| 巫溪县| 海伦市| 达州市| 新平| 利辛县| 喜德县|