mysql數據庫內容相關操做

第一:介紹

mysql數據內容的操做主要是:mysql

  INSERT實現數據的插入sql

  UPDATE實現數據的更新ide

  DLETE實現數據的刪除函數

  SELECT實現數據的查詢。spa

第二:增(insert)

1.插入完整的數據,順序插入3d

insert into 表 (列名,列名) values (值,值)

示例:code

mysql> select * from userinfo;
+-----+------+----------+
| nid | name | password |
+-----+------+----------+
|   1 | root | 123      |
+-----+------+----------+
1 row in set (0.00 sec)

mysql> insert into userinfo(name,password) values('zzl',123);
Query OK, 1 row affected (0.00 sec)

mysql> select * from userinfo;
+-----+------+----------+
| nid | name | password |
+-----+------+----------+
|   1 | root | 123      |
|   4 | zzl  | 123      |
+-----+------+----------+
2 rows in set (0.00 sec)
View Code

2.插入多條:blog

insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)

示例:排序

mysql> select * from userinfo;
+-----+------+----------+
| nid | name | password |
+-----+------+----------+
|   1 | root | 123      |
|   4 | zzl  | 123      |
+-----+------+----------+
2 rows in set (0.00 sec)


mysql> insert into userinfo(name,password) values('zl',123),('cy',123);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from userinfo;
+-----+------+----------+
| nid | name | password |
+-----+------+----------+
|   1 | root | 123      |
|   4 | zzl  | 123      |
|   9 | zl   | 123      |
|  10 | cy   | 123      |
+-----+------+----------+
4 rows in set (0.00 sec)
View Code

3.從其餘表中查詢後插入:ci

insert into 表 (列名,列名...) select (列名,列名...) from

 示例:

mysql> select * from userinfo;
+-----+------+----------+
| nid | name | password |
+-----+------+----------+
|   1 | root | 123      |
|   4 | zzl  | 123      |
|   9 | zl   | 123      |
|  10 | cy   | 123      |
+-----+------+----------+
4 rows in set (0.00 sec)

mysql> select * from user;
Empty set (0.00 sec)

mysql> insert into user(name,password) select name,password from userinfo;
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql>
mysql> select * from user;
+----+------+----------+
| id | name | password |
+----+------+----------+
|  1 | root | 123      |
|  2 | zzl  | 123      |
|  3 | zl   | 123      |
|  4 | cy   | 123      |
+----+------+----------+
4 rows in set (0.00 sec)
View Code

第三:刪

delete from 表名 where name='root';

示例:

mysql> select * from user;
+----+------+----------+
| id | name | password |
+----+------+----------+
|  1 | root | 123      |
|  2 | zzl  | 123      |
|  3 | zl   | 123      |
|  4 | cy   | 123      |
+----+------+----------+
4 rows in set (0.00 sec)

mysql>
mysql> delete from user where name='root';
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+----+------+----------+
| id | name | password |
+----+------+----------+
|  2 | zzl  | 123      |
|  3 | zl   | 123      |
|  4 | cy   | 123      |
+----+------+----------+
3 rows in set (0.00 sec)
View Code

第四:改

update 表 set name='cyy' where id=1

示例:

mysql> select * from user;
+----+------+----------+
| id | name | password |
+----+------+----------+
|  2 | zzl  | 123      |
|  3 | zl   | 123      |
|  4 | cy   | 123      |
+----+------+----------+
3 rows in set (0.01 sec)

mysql> update user set name='cyy' where id=1
    -> ;
Query OK, 0 rows affected (0.01 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> select * from user;
+----+------+----------+
| id | name | password |
+----+------+----------+
|  2 | zzl  | 123      |
|  3 | zl   | 123      |
|  4 | cy   | 123      |
+----+------+----------+
3 rows in set (0.00 sec)
View Code

第五:查

一.單表查詢

1.單表查詢的語法及其優先級:

語法:

SELECT 列名 FROM 表名
                  WHERE 條件
                  GROUP BY field
                  HAVING 篩選
                  ORDER BY field
                  LIMIT 限制條數

優先級:

from:找到具體表
where:拿着where指定的約束條件,去表中取出一條或者多條記錄
group by:將取出的一條或者多條記錄進行分組,若是沒有group by,則總體做爲一組
having:將分組的結果進行having過濾
select:執行查詢
distinct:去重
order by:將結果按照條件排序
limit:限制取出數據
2.簡單查詢:
select * from 表名;
select * from 表名 where id > 3;
select id,name as new_name from 表名 where id > 2;

示例:

mysql> desc user;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(20) | YES  |     | NULL    |                |
| password | varchar(15) | NO   |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> select * from user;
+----+------+----------+
| id | name | password |
+----+------+----------+
|  2 | zzl  | 123      |
|  3 | zl   | 123      |
|  4 | cy   | 123      |
+----+------+----------+
3 rows in set (0.00 sec)

mysql> select * from user where id > 3;
+----+------+----------+
| id | name | password |
+----+------+----------+
|  4 | cy   | 123      |
+----+------+----------+
1 row in set (0.00 sec)

mysql> select id,name as new_name from user where id > 2;
+----+----------+
| id | new_name |
+----+----------+
|  3 | zl       |
|  4 | cy       |
+----+----------+
2 rows in set (0.00 sec)
View Code
3.複雜查詢:
3.1:條件查詢(where):

介紹:where字句中能夠使用如下運算符

1. 比較運算符:> < >= <= <> !=
2. between 80 and 100 值在10到20之間
3. in(80,90,100) 值是10或20或30
4. like 'egon%'
    pattern能夠是%或_,
    %表示任意多字符
    _表示一個字符
5. 邏輯運算符:在多個條件直接能夠使用邏輯運算符 and or not

6.is null 是否爲空

示例:

運算符:
1.單條件查詢:
mysql> select name from user where name='zzl';
+------+
| name |
+------+
| zzl  |
+------+
1 row in set (0.00 sec)

2.多條件查詢:
mysql> select id,name from user where name='zl' and id > 1;
+----+------+
| id | name |
+----+------+
|  3 | zl   |
+----+------+
1 row in set (0.01 sec)

between...and...:
1.獲取between and之間的,包括3也包括4
mysql> select id,name from user where id between 3 and 4;
+----+------+
| id | name |
+----+------+
|  3 | zl   |
|  4 | cy   |
+----+------+
2 rows in set (0.00 sec)

不包括3和4
mysql> select id,name from user where id not between 3 and 4;
+----+------+
| id | name |
+----+------+
|  2 | zzl  |
+----+------+
1 row in set (0.00 sec)

in:
id不是2和3的
mysql> select id,name from user where id not in(2,3);
+----+------+
| id | name |
+----+------+
|  4 | cy   |
+----+------+
1 row in set (0.00 sec)

id是2和3的
mysql> select id,name from user where id in(2,3);
+----+------+
| id | name |
+----+------+
|  2 | zzl  |
|  3 | zl   |
+----+------+
2 rows in set (0.00 sec)

id是2和3的
mysql> select id,name from user where id=2 or id=3;
+----+------+
| id | name |
+----+------+
|  2 | zzl  |
|  3 | zl   |
+----+------+
2 rows in set (0.00 sec)

關鍵字like模糊匹配:
mysql> select id,name from user where name like 'z%';
+----+------+
| id | name |
+----+------+
|  2 | zzl  |
|  3 | zl   |
+----+------+
2 rows in set (0.00 sec)

關鍵字IS NULL(判斷某個字段是否爲NULL不能用等號,須要用IS)
mysql> select id,name from user where name is null;
Empty set (0.01 sec)

mysql> select id,name from user where name is not null;
+----+------+
| id | name |
+----+------+
|  2 | zzl  |
|  3 | zl   |
|  4 | cy   |
+----+------+
3 rows in set (0.00 sec)
3.2:分組(group by):

怎樣用分組:

#一、首先明確一點:分組發生在where以後,即分組是基於where以後獲得的記錄而進行的

#二、分組指的是:將全部記錄按照某個相同字段進行歸類,好比針對員工信息表的職位分組,或者按照性別進行分組等

示例:

mysql> select num from score group by num;
+-----+
| num |
+-----+
|   9 |
|  11 |
|  22 |
|  43 |
|  55 |
|  65 |
|  66 |
|  67 |
|  68 |
|  77 |
|  87 |
|  88 |
|  99 |
| 100 |
+-----+
14 rows in set (0.00 sec)
mysql> select sid,num from score group by sid,num;
+-----+-----+
| sid | num |
+-----+-----+
|   2 |   9 |
|   5 |  55 |
|   8 |  68 |
|   9 |  99 |
|  11 |  66 |
|  12 |  87 |
|  13 |  99 |
|  15 |  11 |
|  16 |  67 |
|  17 | 100 |
|  19 |  11 |
|  20 |  67 |
|  21 | 100 |
|  23 | 100 |
|  24 |  67 |
|  25 | 100 |
|  27 | 100 |
|  28 |  67 |
|  29 |  88 |
|  31 | 100 |
|  32 |  67 |
|  33 |  88 |
|  35 |  88 |
|  36 |  67 |
|  37 |  22 |
|  39 |  77 |
|  40 |  43 |
|  41 |  87 |
|  43 |  77 |
|  44 |  43 |
|  45 |  87 |
|  47 |  77 |
|  48 |  43 |
|  49 |  87 |
|  52 |  87 |
|  54 |  65 |
|  55 |  65 |
|  56 |  65 |
|  57 |  65 |
+-----+-----+
39 rows in set (0.00 sec)

結合where使用
mysql> select sid,num from score where sid > 50 group by sid,num;
+-----+-----+
| sid | num |
+-----+-----+
|  52 |  87 |
|  54 |  65 |
|  55 |  65 |
|  56 |  65 |
|  57 |  65 |
+-----+-----+
5 rows in set (0.00 sec)

結合where 和 order by使用
mysql> select sid,num from score where sid > 50 group by sid,num order by sid; 從小到大排序
+-----+-----+
| sid | num |
+-----+-----+
|  52 |  87 |
|  54 |  65 |
|  55 |  65 |
|  56 |  65 |
|  57 |  65 |
+-----+-----+
5 rows in set (0.00 sec)

mysql> select sid,num from score where sid > 50 group by sid,num order by sid desc; 從大到小排序
+-----+-----+ 
| sid | num |
+-----+-----+
|  57 |  65 |
|  56 |  65 |
|  55 |  65 |
|  54 |  65 |
|  52 |  87 |
+-----+-----+
5 rows in set (0.00 sec)

與having結合使用:
mysql> select sid,num from score where sid > 50 group by sid,num having sid > 55;
+-----+-----+
| sid | num |
+-----+-----+
|  56 |  65 |
|  57 |  65 |
+-----+-----+
2 rows in set (0.01 sec)

與having ,order by結合使用:

mysql> select sid,num from score where sid > 50 group by sid,num having sid > 55 order by sid desc;
+-----+-----+
| sid | num |
+-----+-----+
|  57 |  65 |
|  56 |  65 |
+-----+-----+
2 rows in set (0.00 sec)
View Code
3.3:去重(having):

介紹:

執行優先級從高到低:where > group by > having

1. Where 發生在分組group by以前,於是Where中能夠有任意字段,可是絕對不能使用聚合函數。
2. Having發生在分組group by以後,於是Having中能夠使用分組的字段,沒法直接取到其餘字段,能夠使用聚合函數

示例:

mysql> select sid,num from score where sid>40 group by sid,num having sid > 50;
+-----+-----+
| sid | num |
+-----+-----+
|  52 |  87 |
|  54 |  65 |
|  55 |  65 |
|  56 |  65 |
|  57 |  65 |
+-----+-----+
5 rows in set (0.00 sec)
View Code
3.4:排序(order by):
mysql> select sid,num from score order by num;
+-----+-----+
| sid | num |
+-----+-----+
|   2 |   9 |
|  19 |  11 |
|  15 |  11 |
|  37 |  22 |
|  40 |  43 |
|  48 |  43 |
|  44 |  43 |
|   5 |  55 |
|  54 |  65 |
|  55 |  65 |
|  56 |  65 |
|  57 |  65 |
|  11 |  66 |
|  16 |  67 |
|  20 |  67 |
|  24 |  67 |
|  36 |  67 |
|  28 |  67 |
|  32 |  67 |
|   8 |  68 |
|  39 |  77 |
|  43 |  77 |
|  47 |  77 |
|  12 |  87 |
|  41 |  87 |
|  45 |  87 |
|  49 |  87 |
|  52 |  87 |
|  29 |  88 |
|  33 |  88 |
|  35 |  88 |
|   9 |  99 |
|  13 |  99 |
|  17 | 100 |
|  21 | 100 |
|  23 | 100 |
|  27 | 100 |
|  25 | 100 |
|  31 | 100 |
+-----+-----+
39 rows in set (0.00 sec)

mysql> select sid,num from score order by num asc;
+-----+-----+
| sid | num |
+-----+-----+
|   2 |   9 |
|  19 |  11 |
|  15 |  11 |
|  37 |  22 |
|  40 |  43 |
|  48 |  43 |
|  44 |  43 |
|   5 |  55 |
|  54 |  65 |
|  55 |  65 |
|  56 |  65 |
|  57 |  65 |
|  11 |  66 |
|  16 |  67 |
|  20 |  67 |
|  24 |  67 |
|  36 |  67 |
|  28 |  67 |
|  32 |  67 |
|   8 |  68 |
|  39 |  77 |
|  43 |  77 |
|  47 |  77 |
|  12 |  87 |
|  41 |  87 |
|  45 |  87 |
|  49 |  87 |
|  52 |  87 |
|  29 |  88 |
|  33 |  88 |
|  35 |  88 |
|   9 |  99 |
|  13 |  99 |
|  17 | 100 |
|  21 | 100 |
|  23 | 100 |
|  27 | 100 |
|  25 | 100 |
|  31 | 100 |
+-----+-----+
39 rows in set (0.00 sec)

mysql> select sid,num from score order by num desc;
+-----+-----+
| sid | num |
+-----+-----+
|  31 | 100 |
|  17 | 100 |
|  21 | 100 |
|  23 | 100 |
|  25 | 100 |
|  27 | 100 |
|   9 |  99 |
|  13 |  99 |
|  35 |  88 |
|  33 |  88 |
|  29 |  88 |
|  12 |  87 |
|  41 |  87 |
|  45 |  87 |
|  49 |  87 |
|  52 |  87 |
|  39 |  77 |
|  43 |  77 |
|  47 |  77 |
|   8 |  68 |
|  16 |  67 |
|  32 |  67 |
|  20 |  67 |
|  24 |  67 |
|  28 |  67 |
|  36 |  67 |
|  11 |  66 |
|  57 |  65 |
|  54 |  65 |
|  55 |  65 |
|  56 |  65 |
|   5 |  55 |
|  48 |  43 |
|  40 |  43 |
|  44 |  43 |
|  37 |  22 |
|  15 |  11 |
|  19 |  11 |
|   2 |   9 |
+-----+-----+
39 rows in set (0.00 sec)

若果num相同,則按照sid排序
mysql> select sid,num from score order by num,sid;
+-----+-----+
| sid | num |
+-----+-----+
|   2 |   9 |
|  15 |  11 |
|  19 |  11 |
|  37 |  22 |
|  40 |  43 |
|  44 |  43 |
|  48 |  43 |
|   5 |  55 |
|  54 |  65 |
|  55 |  65 |
|  56 |  65 |
|  57 |  65 |
|  11 |  66 |
|  16 |  67 |
|  20 |  67 |
|  24 |  67 |
|  28 |  67 |
|  32 |  67 |
|  36 |  67 |
|   8 |  68 |
|  39 |  77 |
|  43 |  77 |
|  47 |  77 |
|  12 |  87 |
|  41 |  87 |
|  45 |  87 |
|  49 |  87 |
|  52 |  87 |
|  29 |  88 |
|  33 |  88 |
|  35 |  88 |
|   9 |  99 |
|  13 |  99 |
|  17 | 100 |
|  21 | 100 |
|  23 | 100 |
|  25 | 100 |
|  27 | 100 |
|  31 | 100 |
+-----+-----+
39 rows in set (0.00 sec)
排序
3.5:限制(limit):
#默認初始位置爲0 
mysql> select sid,num from score order by num,sid limit 3;
+-----+-----+
| sid | num |
+-----+-----+
|   2 |   9 |
|  15 |  11 |
|  19 |  11 |
+-----+-----+
3 rows in set (0.01 sec)

#從第0開始,即先查詢出第一條,而後包含這一條在內日後查3條
mysql> select sid,num from score order by num,sid limit 0,3;
+-----+-----+
| sid | num |
+-----+-----+
|   2 |   9 |
|  15 |  11 |
|  19 |  11 |
+-----+-----+
3 rows in set (0.00 sec)

#從第3開始,即先查詢出第3條,而後包含這一條在內日後查5條
mysql> select sid,num from score order by num,sid limit 3,8;
+-----+-----+
| sid | num |
+-----+-----+
|  37 |  22 |
|  40 |  43 |
|  44 |  43 |
|  48 |  43 |
|   5 |  55 |
|  54 |  65 |
|  55 |  65 |
|  56 |  65 |
+-----+-----+
8 rows in set (0.00 sec)
limit
3.6:正則匹配查詢(通配符)

 

mysql> select * from teacher where tname REGEXP '^蒼';
+-----+------------+
| tid | tname      |
+-----+------------+
|   1 | 蒼井空老師 |
+-----+------------+
1 row in set (0.01 sec)

mysql> select * from teacher where tname REGEXP '師$';
+-----+----------------+
| tid | tname          |
+-----+----------------+
|   1 | 蒼井空老師     |
|   2 | 波多野結衣老師 |
|   3 | 飯島愛老師     |
|   4 | 小澤瑪利亞老師 |
|   5 | 武藤蘭老師     |
+-----+----------------+
5 rows in set (0.00 sec)

注:也能夠用like的,上面已經用過,這裏再也不舉例了

 二.多表查詢

1.多表鏈接查詢
外連接語法

SELECT 字段名...
    FROM 表1 INNER|LEFT|RIGHT JOIN 表2
    ON 表1.字段 = 表2.字段;

1.1交叉鏈接:不使用任何匹配條件:

mysql> select * from class,teacher;
+-----+------------+-----+----------------+
| cid | caption    | tid | tname          |
+-----+------------+-----+----------------+
|   1 | 三年級二班 |   1 | 蒼井空老師     |
|   2 | 一年級三班 |   1 | 蒼井空老師     |
|   3 | 三年級一班 |   1 | 蒼井空老師     |
|   1 | 三年級二班 |   2 | 波多野結衣老師 |
|   2 | 一年級三班 |   2 | 波多野結衣老師 |
|   3 | 三年級一班 |   2 | 波多野結衣老師 |
|   1 | 三年級二班 |   3 | 飯島愛老師     |
|   2 | 一年級三班 |   3 | 飯島愛老師     |
|   3 | 三年級一班 |   3 | 飯島愛老師     |
|   1 | 三年級二班 |   4 | 小澤瑪利亞老師 |
|   2 | 一年級三班 |   4 | 小澤瑪利亞老師 |
|   3 | 三年級一班 |   4 | 小澤瑪利亞老師 |
|   1 | 三年級二班 |   5 | 武藤蘭老師     |
|   2 | 一年級三班 |   5 | 武藤蘭老師     |
|   3 | 三年級一班 |   5 | 武藤蘭老師     |
+-----+------------+-----+----------------+
15 rows in set (0.00 sec)
View Code

1.2 內鏈接:只鏈接匹配的行:

 

mysql> select * from class;
+-----+------------+
| cid | caption    |
+-----+------------+
|   1 | 三年級二班 |
|   2 | 一年級三班 |
|   3 | 三年級一班 |
+-----+------------+
3 rows in set (0.00 sec)

mysql> select * from student;
+-----+--------+----------+-------+
| sid | gender | class_id | sname |
+-----+--------+----------+-------+
|   1 | 男     |        1 | 理解  |
|   2 | 女     |        1 | 鋼蛋  |
|   3 | 男     |        1 | 張三  |
|   4 | 男     |        1 | 張一  |
|   5 | 女     |        1 | 張二  |
|   6 | 男     |        1 | 張四  |
|   7 | 女     |        2 | 鐵錘  |
|   8 | 男     |        2 | 李三  |
|   9 | 男     |        2 | 李一  |
|  10 | 女     |        2 | 李二  |
|  11 | 男     |        2 | 李四  |
|  12 | 女     |        3 | 如花  |
|  13 | 男     |        3 | 劉三  |
|  14 | 男     |        3 | 劉一  |
|  15 | 女     |        3 | 劉二  |
|  16 | 男     |        5 | 劉四  |
+-----+--------+----------+-------+
16 rows in set (0.00 sec)

#找兩張表共有的部分,至關於利用條件從笛卡爾積結果中篩選出了正確的結果
#class沒有5這個班級,於是student表中關於16這條學生信息沒有匹配出來

inner: 內鏈接:只鏈接匹配的行
mysql> select * from student inner join class on class.cid = student.class_id;
+-----+--------+----------+-------+-----+------------+
| sid | gender | class_id | sname | cid | caption    |
+-----+--------+----------+-------+-----+------------+
|   1 | 男     |        1 | 理解  |   1 | 三年級二班 |
|   2 | 女     |        1 | 鋼蛋  |   1 | 三年級二班 |
|   3 | 男     |        1 | 張三  |   1 | 三年級二班 |
|   4 | 男     |        1 | 張一  |   1 | 三年級二班 |
|   5 | 女     |        1 | 張二  |   1 | 三年級二班 |
|   6 | 男     |        1 | 張四  |   1 | 三年級二班 |
|   7 | 女     |        2 | 鐵錘  |   2 | 一年級三班 |
|   8 | 男     |        2 | 李三  |   2 | 一年級三班 |
|   9 | 男     |        2 | 李一  |   2 | 一年級三班 |
|  10 | 女     |        2 | 李二  |   2 | 一年級三班 |
|  11 | 男     |        2 | 李四  |   2 | 一年級三班 |
|  12 | 女     |        3 | 如花  |   3 | 三年級一班 |
|  13 | 男     |        3 | 劉三  |   3 | 三年級一班 |
|  14 | 男     |        3 | 劉一  |   3 | 三年級一班 |
|  15 | 女     |        3 | 劉二  |   3 | 三年級一班 |
+-----+--------+----------+-------+-----+------------+
15 rows in set (0.00 sec)

外連接之左鏈接:優先顯示左表所有記錄
以左表爲準,即找出全部學生信息,固然包括沒有班級的學生
#本質就是:在內鏈接的基礎上增長左邊有右邊沒有的結果
mysql> select * from student left join class on class.cid = student.class_id;
+-----+--------+----------+-------+------+------------+
| sid | gender | class_id | sname | cid  | caption    |
+-----+--------+----------+-------+------+------------+
|   1 | 男     |        1 | 理解  |    1 | 三年級二班 |
|   2 | 女     |        1 | 鋼蛋  |    1 | 三年級二班 |
|   3 | 男     |        1 | 張三  |    1 | 三年級二班 |
|   4 | 男     |        1 | 張一  |    1 | 三年級二班 |
|   5 | 女     |        1 | 張二  |    1 | 三年級二班 |
|   6 | 男     |        1 | 張四  |    1 | 三年級二班 |
|   7 | 女     |        2 | 鐵錘  |    2 | 一年級三班 |
|   8 | 男     |        2 | 李三  |    2 | 一年級三班 |
|   9 | 男     |        2 | 李一  |    2 | 一年級三班 |
|  10 | 女     |        2 | 李二  |    2 | 一年級三班 |
|  11 | 男     |        2 | 李四  |    2 | 一年級三班 |
|  12 | 女     |        3 | 如花  |    3 | 三年級一班 |
|  13 | 男     |        3 | 劉三  |    3 | 三年級一班 |
|  14 | 男     |        3 | 劉一  |    3 | 三年級一班 |
|  15 | 女     |        3 | 劉二  |    3 | 三年級一班 |
|  16 | 男     |        5 | 劉四  | NULL | NULL       |
+-----+--------+----------+-------+------+------------+
16 rows in set (0.00 sec)

外連接之右鏈接:優先顯示右表所有記錄
顯示class表的所有相關記錄
mysql> select * from student right join class on class.cid = student.class_id;
+------+--------+----------+-------+-----+------------+
| sid  | gender | class_id | sname | cid | caption    |
+------+--------+----------+-------+-----+------------+
|    1 | 男     |        1 | 理解  |   1 | 三年級二班 |
|    2 | 女     |        1 | 鋼蛋  |   1 | 三年級二班 |
|    3 | 男     |        1 | 張三  |   1 | 三年級二班 |
|    4 | 男     |        1 | 張一  |   1 | 三年級二班 |
|    5 | 女     |        1 | 張二  |   1 | 三年級二班 |
|    6 | 男     |        1 | 張四  |   1 | 三年級二班 |
|    7 | 女     |        2 | 鐵錘  |   2 | 一年級三班 |
|    8 | 男     |        2 | 李三  |   2 | 一年級三班 |
|    9 | 男     |        2 | 李一  |   2 | 一年級三班 |
|   10 | 女     |        2 | 李二  |   2 | 一年級三班 |
|   11 | 男     |        2 | 李四  |   2 | 一年級三班 |
|   12 | 女     |        3 | 如花  |   3 | 三年級一班 |
|   13 | 男     |        3 | 劉三  |   3 | 三年級一班 |
|   14 | 男     |        3 | 劉一  |   3 | 三年級一班 |
|   15 | 女     |        3 | 劉二  |   3 | 三年級一班 |
+------+--------+----------+-------+-----+------------+
15 rows in set (0.00 sec)


全外鏈接:顯示左右兩個表所有記錄
在內鏈接的基礎上增長左邊有右邊沒有的和右邊有左邊沒有的結果
注意:union與union all的區別:union會去掉相同的紀錄

mysql> select * from student left join class on class.cid = student.class_id union select * from student right join class on class.cid = student.class_id;
+------+--------+----------+-------+------+------------+
| sid  | gender | class_id | sname | cid  | caption    |
+------+--------+----------+-------+------+------------+
|    1 | 男     |        1 | 理解  |    1 | 三年級二班 |
|    2 | 女     |        1 | 鋼蛋  |    1 | 三年級二班 |
|    3 | 男     |        1 | 張三  |    1 | 三年級二班 |
|    4 | 男     |        1 | 張一  |    1 | 三年級二班 |
|    5 | 女     |        1 | 張二  |    1 | 三年級二班 |
|    6 | 男     |        1 | 張四  |    1 | 三年級二班 |
|    7 | 女     |        2 | 鐵錘  |    2 | 一年級三班 |
|    8 | 男     |        2 | 李三  |    2 | 一年級三班 |
|    9 | 男     |        2 | 李一  |    2 | 一年級三班 |
|   10 | 女     |        2 | 李二  |    2 | 一年級三班 |
|   11 | 男     |        2 | 李四  |    2 | 一年級三班 |
|   12 | 女     |        3 | 如花  |    3 | 三年級一班 |
|   13 | 男     |        3 | 劉三  |    3 | 三年級一班 |
|   14 | 男     |        3 | 劉一  |    3 | 三年級一班 |
|   15 | 女     |        3 | 劉二  |    3 | 三年級一班 |
|   16 | 男     |        5 | 劉四  | NULL | NULL       |
+------+--------+----------+-------+------+------------+
16 rows in set (0.01 sec)
2.符合條件鏈接查詢
之內鏈接的方式查詢student和class表,而且student表中的gender字段是男生的,即找出男生姓名以及全部男生所在的班級
inner:
mysql> select student.sname,class.caption from student inner join class on class.cid = student.class_id where student.gender='';
+-------+------------+
| sname | caption    |
+-------+------------+
| 理解  | 三年級二班 |
| 張三  | 三年級二班 |
| 張一  | 三年級二班 |
| 張四  | 三年級二班 |
| 李三  | 一年級三班 |
| 李一  | 一年級三班 |
| 李四  | 一年級三班 |
| 劉三  | 三年級一班 |
| 劉一  | 三年級一班 |
+-------+------------+
9 rows in set (0.00 sec)
3.子查詢
#1:子查詢是將一個查詢語句嵌套在另外一個查詢語句中。
#2:內層查詢語句的查詢結果,能夠爲外層查詢語句提供查詢條件。
#3:子查詢中能夠包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等關鍵字
#4:還能夠包含比較運算符:= 、 !=、> 、<等

3.1 帶IN關鍵字的子查詢

mysql> desc student;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| sid      | int(11)     | NO   | PRI | NULL    | auto_increment |
| gender   | char(1)     | NO   |     | NULL    |                |
| class_id | int(11)     | NO   | MUL | NULL    |                |
| sname    | varchar(32) | NO   |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

mysql> desc score;
+------------+---------+------+-----+---------+----------------+
| Field      | Type    | Null | Key | Default | Extra          |
+------------+---------+------+-----+---------+----------------+
| sid        | int(11) | NO   | PRI | NULL    | auto_increment |
| student_id | int(11) | NO   | MUL | NULL    |                |
| course_id  | int(11) | NO   | MUL | NULL    |                |
| num        | int(11) | NO   |     | NULL    |                |
+------------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> desc course;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| cid        | int(11)     | NO   | PRI | NULL    | auto_increment |
| cname      | varchar(32) | NO   |     | NULL    |                |
| teacher_id | int(11)     | NO   | MUL | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> desc teacher;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| tid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| tname | varchar(32) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

查詢學過「小澤瑪利亞」老師所教的全部課的同窗的學號、姓名;
	select * from student where sid in (
		select student_id from score where score.course_id in (
			select cid from teacher left join course on teacher.tid = course.cid where tname = "小澤瑪利亞老師"
			)
		);

3.2 帶比較運算符的子查詢

查詢有課程成績小於60分的同窗的學號、姓名;
    select sid,sname from student where sid in (select student_id from score where num < 60);

3.3 帶EXISTS關鍵字的子查詢

解釋:EXISTS關字鍵字表示存在。在使用EXISTS關鍵字時,內層查詢語句不返回查詢的記錄。
而是返回一個真假值。True或False
當返回True時,外層查詢語句將進行查詢;當返回值爲False時,外層查詢語句不進行查詢

mysql> select * from student where exists (select * from score where num=100);
+-----+--------+----------+-------+
| sid | gender | class_id | sname |
+-----+--------+----------+-------+
|   1 | 男     |        1 | 理解  |
|   2 | 女     |        1 | 鋼蛋  |
|   3 | 男     |        1 | 張三  |
|   4 | 男     |        1 | 張一  |
|   5 | 女     |        1 | 張二  |
|   6 | 男     |        1 | 張四  |
|   7 | 女     |        2 | 鐵錘  |
|   8 | 男     |        2 | 李三  |
|   9 | 男     |        2 | 李一  |
|  10 | 女     |        2 | 李二  |
|  11 | 男     |        2 | 李四  |
|  12 | 女     |        3 | 如花  |
|  13 | 男     |        3 | 劉三  |
|  14 | 男     |        3 | 劉一  |
|  15 | 女     |        3 | 劉二  |
|  16 | 男     |        5 | 劉四  |
+-----+--------+----------+-------+
16 rows in set (0.00 sec)

mysql> select * from student where exists (select * from score where num=0);
Empty set (0.00 sec)
相關文章
相關標籤/搜索