中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

單位網(wǎng)站建設(shè)申請(qǐng)seo1新地址在哪里

單位網(wǎng)站建設(shè)申請(qǐng),seo1新地址在哪里,網(wǎng)站建設(shè)最花時(shí)間的是,織夢(mèng)網(wǎng)站怎么做備份文章目錄表的增刪查改Create(創(chuàng)建)單行數(shù)據(jù) 全列插入多行數(shù)據(jù) 指定列插入插入否則更新替換Retrieve(讀取)SELECT列全列查詢指定列查詢查詢字段為表達(dá)式查詢結(jié)果指定別名結(jié)果去重WHERE 條件基本比較BETWEEN AND 條件連接OR 條件連…

文章目錄

  • 表的增刪查改
    • Create(創(chuàng)建)
      • 單行數(shù)據(jù) + 全列插入
      • 多行數(shù)據(jù) + 指定列插入
      • 插入否則更新
      • 替換
    • Retrieve(讀取)
      • SELECT列
        • 全列查詢
        • 指定列查詢
        • 查詢字段為表達(dá)式
        • 查詢結(jié)果指定別名
        • 結(jié)果去重
      • WHERE 條件
        • 基本比較
        • BETWEEN AND 條件連接
        • OR 條件連接
        • IN 條件連接
        • LIKE 條件匹配
        • WHERE 條件中使用表達(dá)式
        • AND 與 NOT 的使用
        • 綜合性查詢
        • NULL的查詢
      • 結(jié)果排序
        • 升序顯示
        • 降序排序
        • 多字段排序
        • ORDER BY 使用表達(dá)式
        • 結(jié)合 WHERE 子句 和 ORDER BY 子句
      • 篩選分頁結(jié)果
    • Update(更新)
      • 更新單列
      • 更新多列
      • 更新值為原值基礎(chǔ)上變更
        • 更新全表
    • Delete(刪除)
      • 刪除單條記錄
      • 刪除整表
      • 截?cái)啾?/li>
    • 插入查詢結(jié)果
    • 聚合函數(shù)
    • group by子句的使用

表的增刪查改

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

Create(創(chuàng)建)

基本語法:

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

案例:

mysql> create table students (-> id int unsigned primary key auto_increment,-> sn int not null unique comment '學(xué)號(hào)',-> name varchar(20) not null,-> email varchar(20)-> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.03 sec)mysql> desc students;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| sn    | int(11)          | NO   | UNI | NULL    |                |
| name  | varchar(20)      | NO   |     | NULL    |                |
| email | varchar(20)      | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

單行數(shù)據(jù) + 全列插入

插入兩條記錄,當(dāng)value_list 數(shù)量和定義表的列的數(shù)量及順序一致時(shí),就可以省略value_list。注意,這里在插入的時(shí)候,也可以不用指定id,mysql會(huì)使用默認(rèn)的值進(jìn)行自增。

mysql> insert into students values (100, 1000, 'Curry', NULL);
Query OK, 1 row affected (0.01 sec)mysql> insert into students values (101, 1001, 'Durant', '3306@163.com');
Query OK, 1 row affected (0.00 sec)mysql> select * from students;
+-----+------+--------+--------------+
| id  | sn   | name   | email        |
+-----+------+--------+--------------+
| 100 | 1000 | Curry  | NULL         |
| 101 | 1001 | Durant | 3306@163.com |
+-----+------+--------+--------------+
2 rows in set (0.00 sec)

多行數(shù)據(jù) + 指定列插入

插入兩條記錄,value_list 數(shù)量必須和指定列數(shù)量及順序一致

mysql> insert into students (id, sn, name) values (102, 1002, 'Kobe'), (103, 1003, 'Klay');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0mysql> select * from students;
+-----+------+--------+--------------+
| id  | sn   | name   | email        |
+-----+------+--------+--------------+
| 100 | 1000 | Curry  | NULL         |
| 101 | 1001 | Durant | 3306@163.com |
| 102 | 1002 | Kobe   | NULL         |
| 103 | 1003 | Klay   | NULL         |
+-----+------+--------+--------------+
4 rows in set (0.00 sec)

插入否則更新

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

主鍵沖突:

mysql> insert into students (id, sn, name) values (100, 1004, 'Brown');
ERROR 1062 (23000): Duplicate entry '100' for key 'PRIMARY'

唯一鍵沖突:

mysql> insert into students (id, sn, name) values (104, 1003, 'Bryant');
ERROR 1062 (23000): Duplicate entry '1003' for key 'sn'

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

INSERT ... ON DUPLICATE KEY UPDATEcolumn = value [, column = value] ...
mysql> insert into students (id, sn, name) values (104, 1003, 'Bryant')-> on duplicate key update id=104, name='Bryant';
Query OK, 2 rows affected (0.01 sec)mysql> select * from students;
+-----+------+--------+--------------+
| id  | sn   | name   | email        |
+-----+------+--------+--------------+
| 100 | 1000 | Curry  | NULL         |
| 101 | 1001 | Durant | 3306@163.com |
| 102 | 1002 | Kobe   | NULL         |
| 104 | 1003 | Bryant | NULL         |
+-----+------+--------+--------------+
4 rows in set (0.00 sec)
  • 0 row affected: 表中有沖突數(shù)據(jù),但沖突數(shù)據(jù)的值和 update 的值相等
  • 1 row affected: 表中沒有沖突數(shù)據(jù),數(shù)據(jù)被插入
  • 2 row affected: 表中有沖突數(shù)據(jù),并且數(shù)據(jù)已經(jīng)被更新

替換

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

mysql> replace into students (sn, name) values (1002, 'Mitchell');
Query OK, 2 rows affected (0.00 sec)mysql> select * from students;
+-----+------+----------+--------------+
| id  | sn   | name     | email        |
+-----+------+----------+--------------+
| 100 | 1000 | Curry    | NULL         |
| 101 | 1001 | Durant   | 3306@163.com |
| 104 | 1003 | Bryant   | NULL         |
| 105 | 1002 | Mitchell | NULL         |
+-----+------+----------+--------------+
4 rows in set (0.00 sec)
  • 1 row affected: 表中沒有沖突數(shù)據(jù),數(shù)據(jù)被插入
  • 2 row affected: 表中有沖突數(shù)據(jù),刪除后重新插入

Retrieve(讀取)

基礎(chǔ)語法:

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

案例:

創(chuàng)建表結(jié)構(gòu):

mysql> create table exam_result (-> id int unsigned primary key auto_increment,-> name varchar(20) not null comment '姓名',-> chinese float default 0.0 comment '語文成績(jī)',-> math float default 0.0 comment '數(shù)學(xué)成績(jī)',-> english float default 0.0 comment '英語成績(jī)'-> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

插入測(cè)試數(shù)據(jù):

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列

全列查詢

通常情況下不建議使用 * 進(jìn)行全列查詢

  1. 查詢的列越多,意味著需要傳輸?shù)臄?shù)據(jù)量越大;
  2. 可能會(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 id, name, math from exam_result;
+----+-----------+------+
| id | name      | math |
+----+-----------+------+
|  1 | 唐三藏    |   98 |
|  2 | 孫悟空    |   78 |
|  3 | 豬悟能    |   98 |
|  4 | 曹孟德    |   84 |
|  5 | 劉玄德    |   85 |
|  6 | 孫權(quán)      |   73 |
|  7 | 宋公明    |   65 |
+----+-----------+------+
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, math+10 from exam_result;
+----+-----------+---------+
| id | name      | math+10 |
+----+-----------+---------+
|  1 | 唐三藏    |     108 |
|  2 | 孫悟空    |      88 |
|  3 | 豬悟能    |     108 |
|  4 | 曹孟德    |      94 |
|  5 | 劉玄德    |      95 |
|  6 | 孫權(quán)      |      83 |
|  7 | 宋公明    |      75 |
+----+-----------+---------+
7 rows in set (0.00 sec)

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

mysql> select id, name, math+chinese+english from exam_result;
+----+-----------+----------------------+
| id | name      | math+chinese+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é)果指定別名

基礎(chǔ)語法:

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

結(jié)果去重

查詢結(jié)果重復(fù):

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

查詢結(jié)果去重:

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

WHERE 條件

基本比較

英語不及格的同學(xué)及英語成績(jī) ( < 60 ):

mysql> select name, english from exam_result where english<60;
+-----------+---------+
| name      | english |
+-----------+---------+
| 唐三藏    |      56 |
| 劉玄德    |      45 |
| 宋公明    |      30 |
+-----------+---------+
3 rows in set (0.00 sec)

BETWEEN AND 條件連接

語文成績(jī)?cè)?[80, 90] 分的同學(xué)及語文成績(jī):

使用 AND 進(jìn)行條件連接

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)

OR 條件連接

數(shù)學(xué)成績(jī)是 58 或者 59 或者 98 或者 99 分的同學(xué)及數(shù)學(xué)成績(jī):

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 條件連接

數(shù)學(xué)成績(jī)是 58 或者 59 或者 98 或者 99 分的同學(xué)及數(shù)學(xué)成績(jī):

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)

LIKE 條件匹配

查找姓孫的同學(xué):% 匹配任意多個(gè)(包括 0 個(gè))任意字符

mysql> select name from exam_result where name like '孫%';
+-----------+
| name      |
+-----------+
| 孫悟空    |
| 孫權(quán)      |
+-----------+
2 rows in set (0.00 sec)

查找孫某同學(xué): _ 匹配嚴(yán)格的一個(gè)任意字符

mysql> select name from exam_result where name like '孫_';
+--------+
| name   |
+--------+
| 孫權(quán)   |
+--------+
1 row in set (0.00 sec)

WHERE 條件中使用表達(dá)式

總分在 200 分以下的同學(xué):

mysql> select name, chinese+math+english total from exam_result where total<200;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'

這里我們發(fā)現(xiàn)一個(gè)問題,where條件查詢中不能使用指定別名,這是因?yàn)?code>chinese+math+english這個(gè)字句比where total<200字句先執(zhí)行,所以MySQL并不認(rèn)識(shí)total這個(gè)別名,就會(huì)報(bào)錯(cuò)。

正確寫法:

mysql> select name, chinese+math+english total from exam_result where chinese+math+english<200;
+-----------+-------+
| name      | total |
+-----------+-------+
| 劉玄德    |   185 |
| 宋公明    |   170 |
+-----------+-------+
2 rows in set (0.00 sec)

AND 與 NOT 的使用

語文成績(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 并且 語文成績(jī) < 數(shù)學(xué)成績(jī) 并且 英語成績(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 |
+-----------+---------+------+---------+-------+
| 豬悟能    |      88 |   98 |      90 |   276 |
| 孫權(quán)      |      70 |   73 |      78 |   221 |
+-----------+---------+------+---------+-------+
2 rows in set (0.00 sec)

NULL的查詢

查詢 email 號(hào)已知的同學(xué)姓名:

mysql> select name from students where email is not null;
+--------+
| name   |
+--------+
| Durant |
+--------+
1 row in set (0.00 sec)

NULL 和 NULL 的比較,= 和 <=> 的區(qū)別:

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

結(jié)果排序

基本語法:

-- ASC 為升序(從小到大)
-- DESC 為降序(從大到小)
-- 默認(rèn)為 ASC
SELECT ... FROM table_name [WHERE ...]ORDER BY column [ASC|DESC], [...];

升序顯示

查詢姓名及數(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)

降序排序

查詢姓名 及 eamil,按 eamil排序顯示:

mysql> select name,email from students order by email;
+----------+--------------+
| name     | email        |
+----------+--------------+
| Curry    | NULL         |
| Bryant   | NULL         |
| Mitchell | NULL         |
| Durant   | 3306@163.com |
+----------+--------------+
4 rows in set (0.00 sec)

NULL 視為比任何值都小,升序出現(xiàn)在最上面


多字段排序

查詢同學(xué)各門成績(jī),依次按 數(shù)學(xué)降序,英語升序,語文升序的方式顯示:

mysql> select name,chinese,math,english from exam_result order by math desc, english asc, chinese asc;
+-----------+---------+------+---------+
| name      | chinese | math | english |
+-----------+---------+------+---------+
| 唐三藏    |      67 |   98 |      56 |
| 豬悟能    |      88 |   98 |      90 |
| 劉玄德    |      55 |   85 |      45 |
| 曹孟德    |      82 |   84 |      67 |
| 孫悟空    |      87 |   78 |      77 |
| 孫權(quán)      |      70 |   73 |      78 |
| 宋公明    |      75 |   65 |      30 |
+-----------+---------+------+---------+
7 rows in set (0.00 sec)

多字段排序,排序優(yōu)先級(jí)隨書寫順序


ORDER BY 使用表達(dá)式

查詢同學(xué)及總分,由高到低:

mysql> select name, chinese+math+english from exam_result order by chinese+math+english desc;
+-----------+----------------------+
| name      | chinese+math+english |
+-----------+----------------------+
| 豬悟能    |                  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)

結(jié)合 WHERE 子句 和 ORDER BY 子句

查詢姓孫的同學(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)

篩選分頁結(jié)果

基礎(chǔ)語法:

-- 起始下標(biāo)為 0
-- 從 0 開始,篩選 n 條結(jié)果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;-- 從 s 開始,篩選 n 條結(jié)果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n;-- 從 s 開始,篩選 n 條結(jié)果,比第二種用法更明確,建議使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;

建議:對(duì)未知表進(jìn)行查詢時(shí),最好加一條 LIMIT 1,避免因?yàn)楸碇袛?shù)據(jù)過大,查詢?nèi)頂?shù)據(jù)導(dǎo)致數(shù)據(jù)庫(kù)卡死按 id 進(jìn)行分頁,每頁 3 條記錄,分別顯示 第 1、2、3 頁。

案例:第 1 頁:

mysql> select id, name, chinese, math, english from exam_result order by id limit 3 offset 0;
+----+-----------+---------+------+---------+
| 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 頁:

mysql> select id, name, chinese, math, english from exam_result order by id limit 3 offset 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 頁,如果結(jié)果不足 3 個(gè),不會(huì)有影響:

mysql> select id, name, chinese, math, english from exam_result order by id limit 3 offset 6;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)

Update(更新)

基本語法:

UPDATE table_name SET column = expr [, column = expr ...][WHERE ...] [ORDER BY ...] [LIMIT ...]

更新單列

將孫悟空同學(xué)的數(shù)學(xué)成績(jī)變更為 80 分:

mysql> select name, math from exam_result where name='孫悟空';
+-----------+------+
| name      | math |
+-----------+------+
| 孫悟空    |   78 |
+-----------+------+
1 row in set (0.00 sec)mysql> update exam_result set math=80 where name='孫悟空';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select name, math from exam_result where name='孫悟空';
+-----------+------+
| name      | math |
+-----------+------+
| 孫悟空    |   80 |
+-----------+------+
1 row in set (0.00 sec)

更新多列

將曹孟德同學(xué)的數(shù)學(xué)成績(jī)變更為 60 分,語文成績(jī)變更為 70 分:

mysql> select name, math, chinese from exam_result where name='曹孟德';
+-----------+------+---------+
| name      | math | chinese |
+-----------+------+---------+
| 曹孟德    |   84 |      82 |
+-----------+------+---------+
1 row in set (0.00 sec)mysql> update exam_result set math=60, chinese=70 where name='曹孟德';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select name, math, chinese from exam_result where name='曹孟德';
+-----------+------+---------+
| name      | math | chinese |
+-----------+------+---------+
| 曹孟德    |   60 |      70 |
+-----------+------+---------+
1 row in set (0.00 sec)

更新值為原值基礎(chǔ)上變更

將總成績(jī)倒數(shù)前三的 3 位同學(xué)的數(shù)學(xué)成績(jī)加上 30 分:

mysql> update exam_result set math=math+30 order by chinese+math+english limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0mysql> select name, math from exam_result;
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 孫悟空    |   80 |
| 豬悟能    |   98 |
| 曹孟德    |   90 |
| 劉玄德    |  115 |
| 孫權(quán)      |   73 |
| 宋公明    |   95 |
+-----------+------+
7 rows in set (0.00 sec)

更新全表

將所有同學(xué)的語文成績(jī)更新為原來的 2 倍:

mysql> select name, chinese from exam_result;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 唐三藏    |      67 |
| 孫悟空    |      87 |
| 豬悟能    |      88 |
| 曹孟德    |      70 |
| 劉玄德    |      55 |
| 孫權(quán)      |      70 |
| 宋公明    |      75 |
+-----------+---------+
7 rows in set (0.00 sec)mysql> update exam_result set chinese=chinese*2;
Query OK, 7 rows affected (0.00 sec)
Rows matched: 7  Changed: 7  Warnings: 0mysql> select name, chinese from exam_result;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 唐三藏    |     134 |
| 孫悟空    |     174 |
| 豬悟能    |     176 |
| 曹孟德    |     140 |
| 劉玄德    |     110 |
| 孫權(quán)      |     140 |
| 宋公明    |     150 |
+-----------+---------+
7 rows in set (0.00 sec)

Delete(刪除)

基礎(chǔ)語法:

DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]

刪除單條記錄

刪除孫悟空同學(xué)的考試成績(jī):

mysql> select * from exam_result where name='孫悟空';
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  2 | 孫悟空    |     174 |   80 |      77 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)mysql> delete from exam_result where name='孫悟空';
Query OK, 1 row affected (0.00 sec)mysql> select * from exam_result where name='孫悟空';
Empty set (0.00 sec)

刪除整表

注意:刪除整表操作要慎用!

準(zhǔn)備測(cè)試表:

mysql> create table for_delete (-> id int unsigned primary key auto_increment,-> name varchar(20)-> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

插入測(cè)試數(shù)據(jù):

mysql> insert into for_delete (name) values ('a'), ('b'), ('c');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0mysql> select * from for_delete;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  3 | c    |
+----+------+
3 rows in set (0.00 sec)

刪除整表數(shù)據(jù):

mysql> delete from for_delete;
Query OK, 3 rows affected (0.00 sec)mysql> select * from for_delete;
Empty set (0.00 sec)

再插入一條數(shù)據(jù),自增 id 在原值上增長(zhǎng):

mysql> insert into for_delete (name) values ('d');
Query OK, 1 row affected (0.00 sec)mysql> select * from for_delete;
+----+------+
| id | name |
+----+------+
|  4 | d    |
+----+------+
1 row in set (0.00 sec)

查看表結(jié)構(gòu),會(huì)有 AUTO_INCREMENT=n 項(xiàng):

mysql> show create table for_delete \G
*************************** 1. row ***************************Table: for_delete
Create Table: CREATE TABLE `for_delete` (`id` int(10) unsigned 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)

說明:雖然delete語句刪除了整表,但是再向刪除后的表插入時(shí),表中的自增值會(huì)在之前的原數(shù)據(jù)的基礎(chǔ)之上增加。


截?cái)啾?/h3>

基礎(chǔ)語法:

TRUNCATE [TABLE] table_name
  1. TRUNCATE 只能對(duì)整表操作,不能像 DELETE 一樣針對(duì)部分?jǐn)?shù)據(jù)操作;
  2. 實(shí)際上 MySQL 不對(duì)數(shù)據(jù)操作,所以比 DELETE 更快,但是TRUNCATE在刪除數(shù)據(jù)的時(shí)候,并不經(jīng)過真正的事物,所以無法回滾
  3. 會(huì)重置 AUTO_INCREMENT 項(xiàng)

準(zhǔn)備測(cè)試表:

mysql> create table for_truncate (-> id int unsigned primary key auto_increment,-> name varchar(20)-> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

插入測(cè)試數(shù)據(jù):

mysql> insert into for_truncate (name) values ('a'), ('b'), ('c');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0mysql> 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í)際上沒有對(duì)數(shù)據(jù)真正操作:

mysql> truncate for_truncate;
Query OK, 0 rows affected (0.02 sec)mysql> select * from for_truncate;
Empty set (0.00 sec)

再插入一條數(shù)據(jù),自增 id 在重新增長(zhǎng):

mysql> insert into for_truncate (name) values ('d');
Query OK, 1 row affected (0.00 sec)mysql> select * from for_truncate;
+----+------+
| id | name |
+----+------+
|  1 | d    |
+----+------+
1 row in set (0.00 sec)

查看表結(jié)構(gòu),會(huì)有 AUTO_INCREMENT=2 項(xiàng):

mysql> show create table for_truncate \G
*************************** 1. row ***************************Table: for_truncate
Create Table: CREATE TABLE `for_truncate` (`id` int(10) unsigned 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é)果

基礎(chǔ)語法:

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

案例:刪除表中的的重復(fù)記錄,重復(fù)的數(shù)據(jù)只能有一份

創(chuàng)建原數(shù)據(jù)表,插入測(cè)試數(shù)據(jù):

mysql> create table duplicate_table (-> id int,-> name varchar(20)-> );
Query OK, 0 rows affected (0.04 sec)mysql> 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結(jié)構(gòu)一樣:

mysql> create table no_duplicate_table like duplicate_table;
Query OK, 0 rows affected (0.01 sec)mysql> desc no_duplicate_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

將 duplicate_table 的去重?cái)?shù)據(jù)插入到 no_duplicate_table:

mysql> insert into no_duplicate_table select distinct * from duplicate_table;
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0mysql> select * from no_duplicate_table;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+
3 rows in set (0.00 sec)

通過重命名表,實(shí)現(xiàn)原子的去重操作:

mysql> alter table duplicate_table rename to duplicate_table_bak;
Query OK, 0 rows affected (0.00 sec)mysql> alter table no_duplicate_table rename to duplicate_table;
Query OK, 0 rows affected (0.01 sec)

聚合函數(shù)

在這里插入圖片描述

案例:

統(tǒng)計(jì)班級(jí)共有多少同學(xué):

mysql> select count(*) from students;
+----------+
| count(*) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)

統(tǒng)計(jì)班級(jí)收集的 email 有多少:

mysql> select count(email) from students;
+--------------+
| count(email) |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)

統(tǒng)計(jì)本次考試的數(shù)學(xué)成績(jī)分?jǐn)?shù)個(gè)數(shù):

統(tǒng)計(jì)全部成績(jī):

mysql> select count(math) from exam_result;
+-------------+
| count(math) |
+-------------+
|           6 |
+-------------+
1 row in set (0.00 sec)

統(tǒng)計(jì)去重成績(jī)數(shù)量:

mysql> select count(distinct math) from exam_result;
+----------------------+
| count(distinct math) |
+----------------------+
|                    5 |
+----------------------+
1 row in set (0.00 sec)

統(tǒng)計(jì)數(shù)學(xué)成績(jī)總分:

mysql> select sum(math) from exam_result;
+-----------+
| sum(math) |
+-----------+
|       569 |
+-----------+
1 row in set (0.00 sec)

統(tǒng)計(jì)平均總分:

mysql> select avg(chinese+math+english) from exam_result;
+---------------------------+
| avg(chinese+math+english) |
+---------------------------+
|                     297.5 |
+---------------------------+
1 row in set (0.00 sec)

返回英語最高分:

mysql> select max(english) from exam_result;
+--------------+
| max(english) |
+--------------+
|           90 |
+--------------+
1 row in set (0.00 sec)

返回 > 70 分以上的數(shù)學(xué)最低分:

mysql> select min(math) from exam_result where math>70;
+-----------+
| min(math) |
+-----------+
|        73 |
+-----------+
1 row in set (0.00 sec)

group by子句的使用

在select中使用group by 子句可以對(duì)指定列進(jìn)行分組查詢

基本語法:

select column1, column2, .. from table group by column;

案例:

準(zhǔn)備工作,創(chuàng)建一個(gè)雇員信息表

  • EMP員工表
  • DEPT部門表
  • SALGRADE工資等級(jí)表
mysql> desc dept;
+--------+--------------------------+------+-----+---------+-------+
| Field  | Type                     | Null | Key | Default | Extra |
+--------+--------------------------+------+-----+---------+-------+
| deptno | int(2) unsigned zerofill | NO   |     | NULL    |       |
| dname  | varchar(14)              | YES  |     | NULL    |       |
| loc    | varchar(13)              | YES  |     | NULL    |       |
+--------+--------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)mysql> desc emp;
+----------+--------------------------+------+-----+---------+-------+
| Field    | Type                     | Null | Key | Default | Extra |
+----------+--------------------------+------+-----+---------+-------+
| empno    | int(6) unsigned zerofill | NO   |     | NULL    |       |
| ename    | varchar(10)              | YES  |     | NULL    |       |
| job      | varchar(9)               | YES  |     | NULL    |       |
| mgr      | int(4) unsigned zerofill | YES  |     | NULL    |       |
| hiredate | datetime                 | YES  |     | NULL    |       |
| sal      | decimal(7,2)             | YES  |     | NULL    |       |
| comm     | decimal(7,2)             | YES  |     | NULL    |       |
| deptno   | int(2) unsigned zerofill | YES  |     | NULL    |       |
+----------+--------------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)mysql> desc salgrade;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| grade | int(11) | YES  |     | NULL    |       |
| losal | int(11) | YES  |     | NULL    |       |
| hisal | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

顯示每個(gè)部門的平均工資和最高工資:

mysql> select deptno, avg(sal) avg, max(sal) max from emp group by deptno;
+--------+-------------+---------+
| deptno | avg         | max     |
+--------+-------------+---------+
|     10 | 2916.666667 | 5000.00 |
|     20 | 2175.000000 | 3000.00 |
|     30 | 1566.666667 | 2850.00 |
+--------+-------------+---------+
3 rows in set (0.00 sec)

每個(gè)部門的每種崗位的平均工資和最低工資:

mysql> select deptno, job, avg(sal) avg, min(sal) min from emp group by deptno, job;
+--------+-----------+-------------+---------+
| deptno | job       | avg         | min     |
+--------+-----------+-------------+---------+
|     10 | CLERK     | 1300.000000 | 1300.00 |
|     10 | MANAGER   | 2450.000000 | 2450.00 |
|     10 | PRESIDENT | 5000.000000 | 5000.00 |
|     20 | ANALYST   | 3000.000000 | 3000.00 |
|     20 | CLERK     |  950.000000 |  800.00 |
|     20 | MANAGER   | 2975.000000 | 2975.00 |
|     30 | CLERK     |  950.000000 |  950.00 |
|     30 | MANAGER   | 2850.000000 | 2850.00 |
|     30 | SALESMAN  | 1400.000000 | 1250.00 |
+--------+-----------+-------------+---------+
9 rows in set (0.00 sec)

平均工資低于2000的部門和它的平均工資:

mysql> select deptno, avg(sal) avg from emp group by deptno having avg < 2000;
+--------+-------------+
| deptno | avg         |
+--------+-------------+
|     30 | 1566.666667 |
+--------+-------------+
1 row in set (0.00 sec)

having經(jīng)常和group by搭配使用,作用是對(duì)分組進(jìn)行篩選,作用有些像where,但是having通常在數(shù)據(jù)where選擇完,group by進(jìn)行分組,再執(zhí)行having篩選。


http://www.risenshineclean.com/news/22159.html

相關(guān)文章:

  • 網(wǎng)站上傳文件不大于5M定么做百度收錄怎么查詢
  • 蛋糕網(wǎng)站內(nèi)容規(guī)劃網(wǎng)絡(luò)營(yíng)銷工程師是做什么的
  • 購(gòu)物網(wǎng)站建設(shè)的可行性內(nèi)部?jī)?yōu)化
  • 天津自貿(mào)區(qū)建設(shè)局網(wǎng)站關(guān)鍵詞出價(jià)計(jì)算公式
  • 傳媒公司做網(wǎng)站條件百度關(guān)鍵詞推廣方案
  • 機(jī)械行業(yè)營(yíng)銷型網(wǎng)站成都搜狗seo
  • 自己做銷售獨(dú)立網(wǎng)站網(wǎng)站運(yùn)營(yíng)及推廣方案
  • dw不用代碼做網(wǎng)站網(wǎng)絡(luò)營(yíng)銷的推廣方法
  • 西安優(yōu)秀的集團(tuán)門戶網(wǎng)站建設(shè)服務(wù)商長(zhǎng)沙網(wǎng)站推廣
  • 石家莊網(wǎng)站建設(shè)求職簡(jiǎn)歷怎么申請(qǐng)網(wǎng)站空間
  • 甘孜商城網(wǎng)站建設(shè)seo實(shí)戰(zhàn)培訓(xùn)機(jī)構(gòu)
  • 沒有外貿(mào)網(wǎng)站 如果做外貿(mào)專業(yè)網(wǎng)絡(luò)推廣
  • 網(wǎng)站子頁面如何做seo經(jīng)典模板網(wǎng)站建設(shè)
  • 網(wǎng)站如何做才能被360收錄營(yíng)銷推廣軟件
  • 個(gè)人企業(yè)網(wǎng)站怎么建設(shè)seo外鏈資源
  • 網(wǎng)站續(xù)費(fèi)收多少合適營(yíng)銷手段有哪些
  • 扁平化企業(yè)網(wǎng)站模板賬號(hào)權(quán)重查詢?nèi)肟谡鹃L(zhǎng)工具
  • 網(wǎng)站信息向上滾動(dòng)標(biāo)簽網(wǎng)頁設(shè)計(jì)與制作代碼成品
  • 寶盈集團(tuán)直營(yíng)網(wǎng)站怎么做什么是網(wǎng)絡(luò)營(yíng)銷平臺(tái)
  • 我的網(wǎng)站為什么打不開喬拓云建站平臺(tái)
  • 制作網(wǎng)站賺錢嗎足球比賽統(tǒng)計(jì)數(shù)據(jù)
  • 好看的網(wǎng)站設(shè)計(jì)網(wǎng)站seo怎么優(yōu)化關(guān)鍵詞排名培訓(xùn)
  • 做外貿(mào)自己的公司網(wǎng)站成品app直播源碼有什么用
  • spring boot 網(wǎng)站開發(fā)網(wǎng)站編輯
  • 知道域名怎么進(jìn)入網(wǎng)站北京網(wǎng)站建設(shè)公司報(bào)價(jià)
  • 圣輝友聯(lián)劉金鵬做網(wǎng)站鄭州網(wǎng)站關(guān)鍵詞優(yōu)化公司哪家好
  • 供應(yīng)長(zhǎng)沙手機(jī)網(wǎng)站建設(shè)天津關(guān)鍵詞排名推廣
  • wordpress 修改登錄地址seo的定義是什么
  • 企業(yè)網(wǎng)站模板下載需謹(jǐn)慎百度信息流投放在哪些平臺(tái)
  • 上傳自己做的網(wǎng)站后臺(tái)怎么辦常見的網(wǎng)絡(luò)營(yíng)銷方式有哪些