日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

1. 開始之前

-- ----------------------------
-- Table structure for dept
-- ----------------------------
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
  `did` int(11) NOT NULL AUTO_INCREMENT COMMENT '部門ID',
  `dname` varchar(60) DEFAULT NULL COMMENT '部門名稱',
  PRIMARY KEY (`did`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of dept
-- ----------------------------
BEGIN;
INSERT INTO `dept` VALUES (1, '研發(fā)部');
INSERT INTO `dept` VALUES (2, '人事部');
INSERT INTO `dept` VALUES (3, '測試部');
INSERT INTO `dept` VALUES (4, '銷售部');
INSERT INTO `dept` VALUES (5, '生產(chǎn)部');
COMMIT;

-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `name` varchar(60) NOT NULL COMMENT '姓名',
  `age` tinyint(4) DEFAULT NULL COMMENT '年齡',
  `sex` tinyint(2) NOT NULL DEFAULT '1' COMMENT '性別,1男,2女',
  `salary` decimal(10,2) NOT NULL COMMENT '薪資',
  `hire_date` date NOT NULL COMMENT '聘用日期',
  `dept_id` int(11) DEFAULT NULL COMMENT '部門ID',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of employee
-- ----------------------------
BEGIN;
INSERT INTO `employee` VALUES (1, '菜虛鯤', 20, 2, 10000.00, '2020-01-10', 1);
INSERT INTO `employee` VALUES (2, '奧力給', 30, 1, 18000.00, '2020-01-08', 1);
INSERT INTO `employee` VALUES (3, '老八', 28, 1, 7000.00, '2020-01-07', 1);
INSERT INTO `employee` VALUES (4, '小張', 25, 1, 8000.00, '2020-01-10', 1);
INSERT INTO `employee` VALUES (5, '小紅', 20, 2, 6000.00, '2020-01-05', 2);
INSERT INTO `employee` VALUES (6, '小麗', 23, 2, 6500.00, '2020-01-05', 2);
INSERT INTO `employee` VALUES (7, '小花', 21, 2, 5500.00, '2020-01-10', 2);
INSERT INTO `employee` VALUES (8, '馬小跳', 25, 1, 7000.00, '2020-01-01', 3);
INSERT INTO `employee` VALUES (9, '張大騷', 30, 1, 9000.00, '2020-01-07', 3);
INSERT INTO `employee` VALUES (10, '馬冬梅', 31, 2, 5000.00, '2020-01-07', 4);
INSERT INTO `employee` VALUES (11, '川堅果', 60, 1, 100.00, '2020-01-08', NULL);
COMMIT;

2. 多表聯(lián)合查詢

2.1 語法

select 字段1,字段2... from 表1,表2... [where 條件]

2.2 實戰(zhàn)

注意:多表聯(lián)合查詢需要添加條件,否則會直接輸出 左表*右表,這種結(jié)果稱之為笛卡爾乘積。

MySQL多表查詢講解

 

集合A中的數(shù)據(jù)乘以集合B中的數(shù)據(jù)等于笛卡爾乘積

MySQL> select * from employee,dept;
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
| id | name      | age | sex | salary   | hire_date  | dept_id | did | dname     |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |   2 | 人事部 |
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |   3 | 測試部 |
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |   4 | 銷售部 |
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |   5 | 生產(chǎn)部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |   1 | 研發(fā)部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |   2 | 人事部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |   3 | 測試部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |   4 | 銷售部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |   5 | 生產(chǎn)部 |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 |   1 | 研發(fā)部 |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 |   2 | 人事部 |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 |   3 | 測試部 |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 |   4 | 銷售部 |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 |   5 | 生產(chǎn)部 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |   2 | 人事部 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |   3 | 測試部 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |   4 | 銷售部 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |   5 | 生產(chǎn)部 |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 |   1 | 研發(fā)部 |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 |   2 | 人事部 |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 |   3 | 測試部 |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 |   4 | 銷售部 |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 |   5 | 生產(chǎn)部 |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 |   1 | 研發(fā)部 |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 |   2 | 人事部 |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 |   3 | 測試部 |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 |   4 | 銷售部 |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 |   5 | 生產(chǎn)部 |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 |   1 | 研發(fā)部 |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 |   2 | 人事部 |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 |   3 | 測試部 |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 |   4 | 銷售部 |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 |   5 | 生產(chǎn)部 |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 |   1 | 研發(fā)部 |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 |   2 | 人事部 |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 |   3 | 測試部 |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 |   4 | 銷售部 |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 |   5 | 生產(chǎn)部 |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 |   1 | 研發(fā)部 |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 |   2 | 人事部 |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 |   3 | 測試部 |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 |   4 | 銷售部 |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 |   5 | 生產(chǎn)部 |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 |   1 | 研發(fā)部 |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 |   2 | 人事部 |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 |   3 | 測試部 |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 |   4 | 銷售部 |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 |   5 | 生產(chǎn)部 |
| 11 | 川堅果 |  60 |   1 | 100.00   | 2020-01-08 | NULL    |   1 | 研發(fā)部 |
| 11 | 川堅果 |  60 |   1 | 100.00   | 2020-01-08 | NULL    |   2 | 人事部 |
| 11 | 川堅果 |  60 |   1 | 100.00   | 2020-01-08 | NULL    |   3 | 測試部 |
| 11 | 川堅果 |  60 |   1 | 100.00   | 2020-01-08 | NULL    |   4 | 銷售部 |
| 11 | 川堅果 |  60 |   1 | 100.00   | 2020-01-08 | NULL    |   5 | 生產(chǎn)部 |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
55 rows in set (0.00 sec)

正確的查詢方式是:以兩表中相互關(guān)聯(lián)的字段作為查詢條件進(jìn)行查詢。

mysql> select * from employee,dept where employee.dept_id = dept.did;

查詢結(jié)果

+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
| id | name      | age | sex | salary   | hire_date  | dept_id | did | dname     |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |   1 | 研發(fā)部 |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 |   1 | 研發(fā)部 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 |   2 | 人事部 |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 |   2 | 人事部 |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 |   2 | 人事部 |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 |   3 | 測試部 |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 |   3 | 測試部 |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 |   4 | 銷售部 |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
10 rows in set (0.00 sec)

輸出結(jié)果為兩個集合的交集。

MySQL多表查詢講解

 

3. Innner內(nèi)連接查詢

3.1 語法

select 字段1,字段2... from 表1 inner join 表2 on [條件];

3.2 實戰(zhàn)

查詢員工和部門信息

select * from employee inner join dept on employee.dept_id = dept.did; 

輸出結(jié)果

mysql> select * from employee inner join dept on employee.dept_id = dept.did; 
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
| id | name      | age | sex | salary   | hire_date  | dept_id | did | dname     |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |   1 | 研發(fā)部 |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 |   1 | 研發(fā)部 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 |   2 | 人事部 |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 |   2 | 人事部 |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 |   2 | 人事部 |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 |   3 | 測試部 |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 |   3 | 測試部 |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 |   4 | 銷售部 |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
10 rows in set (0.04 sec)

其輸出結(jié)果和多表聯(lián)合查詢一致。

MySQL多表查詢講解

 

如果附加其他條件,可以直接用and連接符連接在on語句的后面

mysql> select * from employee inner join dept on employee.dept_id = dept.did and employee.salary >= 10000; 

輸出結(jié)果

+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
| id | name      | age | sex | salary   | hire_date  | dept_id | did | dname     |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |   1 | 研發(fā)部 |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
2 rows in set (0.00 sec)

4. Left左外連接查詢

左外連接查詢,即左表的數(shù)據(jù)全部顯示。

4.1 語法

select * from 表1 left join 表2 on [條件];

4.2 實戰(zhàn)

查詢員工和部門的所有信息

select * from employee left join dept on employee.dept_id = dept.did;

輸出結(jié)果

mysql> select * from employee left join dept on employee.dept_id = dept.did;
+----+-----------+-----+-----+----------+------------+---------+------+-----------+
| id | name      | age | sex | salary   | hire_date  | dept_id | did  | dname     |
+----+-----------+-----+-----+----------+------------+---------+------+-----------+
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |    1 | 研發(fā)部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |    1 | 研發(fā)部 |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 |    1 | 研發(fā)部 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |    1 | 研發(fā)部 |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 |    2 | 人事部 |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 |    2 | 人事部 |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 |    2 | 人事部 |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 |    3 | 測試部 |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 |    3 | 測試部 |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 |    4 | 銷售部 |
| 11 | 川堅果 |  60 |   1 | 100.00   | 2020-01-08 | NULL    | NULL | NULL      |
+----+-----------+-----+-----+----------+------------+---------+------+-----------+
11 rows in set (0.00 sec)

左表中的數(shù)據(jù)全部顯示,右表中的數(shù)據(jù)只顯示符合條件的,不符合條件的以NULL填充

MySQL多表查詢講解

 

更直觀的展現(xiàn)上述關(guān)系

mysql> select * from employee left join dept on employee.dept_id = dept.did and dept.did = 1;
+----+-----------+-----+-----+----------+------------+---------+------+-----------+
| id | name      | age | sex | salary   | hire_date  | dept_id | did  | dname     |
+----+-----------+-----+-----+----------+------------+---------+------+-----------+
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |    1 | 研發(fā)部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |    1 | 研發(fā)部 |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 |    1 | 研發(fā)部 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |    1 | 研發(fā)部 |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 | NULL | NULL      |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 | NULL | NULL      |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 | NULL | NULL      |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 | NULL | NULL      |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 | NULL | NULL      |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 | NULL | NULL      |
| 11 | 川堅果 |  60 |   1 | 100.00   | 2020-01-08 | NULL    | NULL | NULL      |
+----+-----------+-----+-----+----------+------------+---------+------+-----------+
11 rows in set (0.01 sec)

5. Right右外連接查詢

右外連接查詢,與左外連接查詢正好相反,即右表的數(shù)據(jù)全部顯示。

5.1 語法

select * from 表1 right join 表2 on [條件];

5.2 實戰(zhàn)

查詢員工和部門信息

select * from employee right join dept on employee.dept_id = dept.did;

輸出結(jié)果

mysql> select * from employee right join dept on employee.dept_id = dept.did;
+------+-----------+------+------+----------+------------+---------+-----+-----------+
| id   | name      | age  | sex  | salary   | hire_date  | dept_id | did | dname     |
+------+-----------+------+------+----------+------------+---------+-----+-----------+
|    1 | 菜虛鯤 |   20 |    2 | 10000.00 | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|    2 | 奧力給 |   30 |    1 | 18000.00 | 2020-01-08 |       1 |   1 | 研發(fā)部 |
|    3 | 老八    |   28 |    1 | 7000.00  | 2020-01-07 |       1 |   1 | 研發(fā)部 |
|    4 | 小張    |   25 |    1 | 8000.00  | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|    5 | 小紅    |   20 |    2 | 6000.00  | 2020-01-05 |       2 |   2 | 人事部 |
|    6 | 小麗    |   23 |    2 | 6500.00  | 2020-01-05 |       2 |   2 | 人事部 |
|    7 | 小花    |   21 |    2 | 5500.00  | 2020-01-10 |       2 |   2 | 人事部 |
|    8 | 馬小跳 |   25 |    1 | 7000.00  | 2020-01-01 |       3 |   3 | 測試部 |
|    9 | 張大騷 |   30 |    1 | 9000.00  | 2020-01-07 |       3 |   3 | 測試部 |
|   10 | 馬冬梅 |   31 |    2 | 5000.00  | 2020-01-07 |       4 |   4 | 銷售部 |
| NULL | NULL      | NULL | NULL | NULL     | NULL       | NULL    |   5 | 生產(chǎn)部 |
+------+-----------+------+------+----------+------------+---------+-----+-----------+
11 rows in set (0.00 sec)

6. UNION全連接查詢

全連接查詢:其結(jié)果是在內(nèi)連接查詢的基礎(chǔ)上顯示左右兩邊沒有的數(shù)據(jù)。
寫法:左連接查詢 UNION 右連接查詢

6.1 語法

select * from 表1 left join 表2 on [條件]
union
select * from 表1 right join 表2 on [條件]

6.2 實戰(zhàn)

以全連接的形式查詢部門和員工數(shù)據(jù)

select * from employee left join dept on employee.dept_id = dept.did
 union
select * from employee right join dept on employee.dept_id = dept.did;

輸出結(jié)果

mysql> select * from employee left join dept on employee.dept_id = dept.did
    -> union
    -> select * from employee right join dept on employee.dept_id = dept.did;
+------+-----------+------+------+----------+------------+---------+------+-----------+
| id   | name      | age  | sex  | salary   | hire_date  | dept_id | did  | dname     |
+------+-----------+------+------+----------+------------+---------+------+-----------+
|    1 | 菜虛鯤 |   20 |    2 | 10000.00 | 2020-01-10 |       1 |    1 | 研發(fā)部 |
|    2 | 奧力給 |   30 |    1 | 18000.00 | 2020-01-08 |       1 |    1 | 研發(fā)部 |
|    3 | 老八    |   28 |    1 | 7000.00  | 2020-01-07 |       1 |    1 | 研發(fā)部 |
|    4 | 小張    |   25 |    1 | 8000.00  | 2020-01-10 |       1 |    1 | 研發(fā)部 |
|    5 | 小紅    |   20 |    2 | 6000.00  | 2020-01-05 |       2 |    2 | 人事部 |
|    6 | 小麗    |   23 |    2 | 6500.00  | 2020-01-05 |       2 |    2 | 人事部 |
|    7 | 小花    |   21 |    2 | 5500.00  | 2020-01-10 |       2 |    2 | 人事部 |
|    8 | 馬小跳 |   25 |    1 | 7000.00  | 2020-01-01 |       3 |    3 | 測試部 |
|    9 | 張大騷 |   30 |    1 | 9000.00  | 2020-01-07 |       3 |    3 | 測試部 |
|   10 | 馬冬梅 |   31 |    2 | 5000.00  | 2020-01-07 |       4 |    4 | 銷售部 |
|   11 | 川堅果 |   60 |    1 | 100.00   | 2020-01-08 | NULL    | NULL | NULL      |
| NULL | NULL      | NULL | NULL | NULL     | NULL       | NULL    |    5 | 生產(chǎn)部 |
+------+-----------+------+------+----------+------------+---------+------+-----------+
12 rows in set (0.00 sec)

7. 嵌套查詢

嵌套查詢:在一個sql語句中使用多個select,第一次的查詢結(jié)果可作為第二次查詢結(jié)果的條件/表名使用。

7.1 作為表名使用

select * from (select id,name,age from employee) as em where em.id = 1;

查詢結(jié)果

mysql> select * from (select id,name,age from employee) as em where em.id = 1;
+----+-----------+-----+
| id | name      | age |
+----+-----------+-----+
|  1 | 菜虛鯤 |  20 |
+----+-----------+-----+
1 row in set (0.00 sec)

解釋:把(select id,name,age from employee)語句的返回結(jié)果當(dāng)作一個臨時表,臨時表的表名為em。

舉例:查詢平均工資在7000以上的部門
解題思路:
第一步,求部門平均工資

mysql> select AVG(salary) as ag,dept.dname from employee,dept where employee.dept_id = dept.did group by dept.did;
+--------------+-----------+
| ag           | dname     |
+--------------+-----------+
| 10750.000000 | 研發(fā)部 |
| 6000.000000  | 人事部 |
| 8000.000000  | 測試部 |
| 5000.000000  | 銷售部 |
+--------------+-----------+
4 rows in set (0.00 sec)

第二步,把部門平均工資表當(dāng)作臨時表,進(jìn)行查詢。

select dname from 
		(
			select 
				AVG(salary) as ag,
				dept.dname 
				from employee,dept w
				here employee.dept_id = dept.did 
				group by dept.did
		) as dept_avg_salary 
	where dept_avg_salary.ag > 7000;

運行結(jié)果:

mysql> select dname from (select AVG(salary) as ag,dept.dname from employee,dept where employee.dept_id = dept.did group by dept.did) as dept_avg_salary where dept_avg_salary.ag > 7000;
+-----------+
| dname     |
+-----------+
| 研發(fā)部 |
| 測試部 |
+-----------+
2 rows in set (0.00 sec)

7.2 作為查詢條件使用

舉例:找出工資最高的員工的所有信息

select * from employee,dept 
	where employee.dept_id = dept.did 
	and employee.salary = (select MAX(salary) from employee);

查詢結(jié)果

mysql> select * from employee,dept where employee.dept_id = dept.did and employee.salary = (select MAX(salary) from employee);
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
| id | name      | age | sex | salary   | hire_date  | dept_id | did | dname     |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |   1 | 研發(fā)部 |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
1 row in set (0.01 sec)

舉例:求工資大于所有人平均工資的員工的所有信息

select * from employee,dept 
	where employee.dept_id = dept.did 
	and employee.salary > (select AVG(salary) from employee);

查詢結(jié)果

mysql> select * from employee,dept where employee.dept_id = dept.did and employee.salary > (select AVG(salary) from employee);
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
| id | name      | age | sex | salary   | hire_date  | dept_id | did | dname     |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |   1 | 研發(fā)部 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |   1 | 研發(fā)部 |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 |   3 | 測試部 |
+----+-----------+-----+-----+----------+------------+---------+-----+-----------+
4 rows in set (0.00 sec)

7.3. 嵌套查詢關(guān)鍵字

7.3.1 ANY、SOME

兩者意義相同,與比較運算符(=、>、>=、<、<=、<>)結(jié)合起來使用,any/some作用于子查詢語句,只要子查詢語句中有一個符合條件,就返回true。

select s1 from t1 where s1 > any (select s1 from t2);

假設(shè)子查詢語句返回結(jié)果有三個result1,result2,result3,則上述語句等同于:

select s1 from t1 where s1 > result1 or s1 > result2 or s1 > result3;

7.3.2 IN

in 與 =any 相同。相當(dāng)于:

select s1 from t1 where s1 = result1 or s1 = result2 or s1 = result3;

7.3.3 ALL

當(dāng)子查詢語句中的所有項都符合條件時,才返回true。

select s1 from t1 where s1 > all (select s1 from t2);

等同于:

select s1 from t1 where s1 > result1 and s1 > result2 and s1 > result3;

7.3.4 EXISTS、NOT EXISTS

語法:

select ... from tableName exists(subquery);

當(dāng)子查詢語句subquery返回列時,exists表達(dá)式為true,此時執(zhí)行前面的查詢語句。子查詢語句沒有返回任何列時,exists語句為false,不執(zhí)行前面的查詢語句。

mysql> select * from employee where exists (select * from employee where id =1);
+----+-----------+-----+-----+----------+------------+---------+
| id | name      | age | sex | salary   | hire_date  | dept_id |
+----+-----------+-----+-----+----------+------------+---------+
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 |
| 11 | 川堅果 |  60 |   1 | 100.00   | 2020-01-08 | NULL    |
+----+-----------+-----+-----+----------+------------+---------+
11 rows in set (0.00 sec)

當(dāng)子查詢語句沒有返回任何列時

mysql> select * from employee where exists (select * from employee where id =12);
Empty set

select NULL 返回了列,所以:

mysql> select NULL;
+------+
| NULL |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
 
mysql> select * from employee where exists (select NULL) and salary > 10000;
+----+-----------+-----+-----+----------+------------+---------+
| id | name      | age | sex | salary   | hire_date  | dept_id |
+----+-----------+-----+-----+----------+------------+---------+
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 |
+----+-----------+-----+-----+----------+------------+---------+
1 row in set (0.00 sec)

ot exists與exists相反。

8 判斷查詢

8.1 IF

語法:

if(條件表達(dá)式,"結(jié)果為true","結(jié)果為false");

舉例,將薪資大于大于8000的員工薪資級別設(shè)置為小康,小于8000設(shè)置為一般:

select *,if(salary > 8000,"小康","一般") as salary_level from employee;

輸出結(jié)果:

mysql> select *,if(salary > 8000,"小康","一般") as salary_level from employee;
+----+-----------+-----+-----+----------+------------+---------+--------------+
| id | name      | age | sex | salary   | hire_date  | dept_id | salary_level |
+----+-----------+-----+-----+----------+------------+---------+--------------+
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 | 小康       |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 | 小康       |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 | 一般       |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 | 一般       |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 | 一般       |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 | 一般       |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 | 一般       |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 | 一般       |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 | 小康       |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 | 一般       |
| 11 | 川堅果 |  60 |   1 | 100.00   | 2020-01-08 | NULL    | 一般       |
+----+-----------+-----+-----+----------+------------+---------+--------------+
11 rows in set (0.00 sec)

8.2 if … else if … else 形式

語法:

select ...,case when [條件1] then [result]
				when [條件2] then [result]
				else [result]
				end
from tableName;

舉例:
根據(jù)員工工資劃分員工生活水平,小于7000為貧窮,7000 到 9000為一般,9000-10000為中等,10000-12000為中等偏上,大于12000為有錢。

mysql> select name,salary,case  when salary < 7000 then '貧窮'
    -> 							when salary < 9000 then '一般'
    -> 							when salary < 10000 then '中等'
    -> 							when salary < 12000 then '中等偏上'
    -> 							else '有錢'
    -> 					  end as living_standard
    -> from employee;
+-----------+----------+-----------------+
| name      | salary   | living_standard |
+-----------+----------+-----------------+
| 菜虛鯤 | 10000.00 | 中等偏上    |
| 奧力給 | 18000.00 | 有錢          |
| 老八    | 7000.00  | 一般          |
| 小張    | 8000.00  | 一般          |
| 小紅    | 6000.00  | 貧窮          |
| 小麗    | 6500.00  | 貧窮          |
| 小花    | 5500.00  | 貧窮          |
| 馬小跳 | 7000.00  | 一般          |
| 張大騷 | 9000.00  | 中等          |
| 馬冬梅 | 5000.00  | 貧窮          |
| 川堅果 | 100.00   | 貧窮          |
+-----------+----------+-----------------+
11 rows in set (0.00 sec)

其形式類似于if…else if…else if…else形式。

8.3 switch case形式

語法:

select ..., case s1
			when [value1] then [result1]
			when [value2] then [result2]
			when [value3] then [result3]
			else [resultOther] end
from tableName;

舉例,直接顯示出員工性別:

mysql> select *,case sex
    -> 				when 1 then '男'
    -> 				when 2 then '女'
    -> 				else '未知' end as employee_sex
    -> from employee;
+----+-----------+-----+-----+----------+------------+---------+--------------+
| id | name      | age | sex | salary   | hire_date  | dept_id | employee_sex |
+----+-----------+-----+-----+----------+------------+---------+--------------+
|  1 | 菜虛鯤 |  20 |   2 | 10000.00 | 2020-01-10 |       1 | 女          |
|  2 | 奧力給 |  30 |   1 | 18000.00 | 2020-01-08 |       1 | 男          |
|  3 | 老八    |  28 |   1 | 7000.00  | 2020-01-07 |       1 | 男          |
|  4 | 小張    |  25 |   1 | 8000.00  | 2020-01-10 |       1 | 男          |
|  5 | 小紅    |  20 |   2 | 6000.00  | 2020-01-05 |       2 | 女          |
|  6 | 小麗    |  23 |   2 | 6500.00  | 2020-01-05 |       2 | 女          |
|  7 | 小花    |  21 |   2 | 5500.00  | 2020-01-10 |       2 | 女          |
|  8 | 馬小跳 |  25 |   1 | 7000.00  | 2020-01-01 |       3 | 男          |
|  9 | 張大騷 |  30 |   1 | 9000.00  | 2020-01-07 |       3 | 男          |
| 10 | 馬冬梅 |  31 |   2 | 5000.00  | 2020-01-07 |       4 | 女          |
| 11 | 川堅果 |  60 |   1 | 100.00   | 2020-01-08 | NULL    | 男          |
+----+-----------+-----+-----+----------+------------+---------+--------------+
11 rows in set (0.00 sec)

分享到:
標(biāo)簽:查詢
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達(dá)人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定