概述
對(duì)于中小體量的項(xiàng)目而言,聯(lián)表查詢是再常見不過的操作了,尤其是在做報(bào)表的時(shí)候。然而校對(duì)數(shù)據(jù)的時(shí)候,您發(fā)現(xiàn)坑了嗎?本篇文章就 mysql 常用聯(lián)表查詢復(fù)現(xiàn)常見的坑。
基礎(chǔ)環(huán)境
建表語(yǔ)句
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_name` VARCHAR(50) DEFAULT NULL COMMENT '角色名',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色表';
insert into `role` VALUES(1, '管理員');
insert into `role` VALUES(2, '總經(jīng)理');
insert into `role` VALUES(3, '科長(zhǎng)');
insert into `role` VALUES(4, '組長(zhǎng)');
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_id` int(11) NOT NULL COMMENT '角色id',
`user_name` VARCHAR(50) DEFAULT NULL COMMENT '用戶名',
`sex` int(1) DEFAULT 0 COMMENT '性別',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用戶表';
insert into `user` VALUES(1, 1, 'admin', 1);
insert into `user` VALUES(2, 2, '王經(jīng)理', 1);
insert into `user` VALUES(3, 2, '李經(jīng)理', 2);
insert into `user` VALUES(4, 2, '張經(jīng)理', 2);
insert into `user` VALUES(5, 3, '王科長(zhǎng)', 1);
insert into `user` VALUES(6, 3, '李科長(zhǎng)', 1);
insert into `user` VALUES(7, 3, '呂科長(zhǎng)', 2);
insert into `user` VALUES(8, 3, '邢科長(zhǎng)', 1);
insert into `user` VALUES(9, 4, '范組長(zhǎng)', 2);
insert into `user` VALUES(10, 4, '趙組長(zhǎng)', 2);
insert into `user` VALUES(11, 4, '姬組長(zhǎng)', 1);
數(shù)據(jù)如下
mysql> select * from role;
+----+-----------+
| id | role_name |
+----+-----------+
| 1 | 管理員 |
| 2 | 總經(jīng)理 |
| 3 | 科長(zhǎng) |
| 4 | 組長(zhǎng) |
+----+-----------+
4 rows in set (0.00 sec)
mysql> select * from user;
+----+---------+-----------+------+
| id | role_id | user_name | sex |
+----+---------+-----------+------+
| 1 | 1 | admin | 1 |
| 2 | 2 | 王經(jīng)理 | 1 |
| 3 | 2 | 李經(jīng)理 | 2 |
| 4 | 2 | 張經(jīng)理 | 2 |
| 5 | 3 | 王科長(zhǎng) | 1 |
| 6 | 3 | 李科長(zhǎng) | 1 |
| 7 | 3 | 呂科長(zhǎng) | 2 |
| 8 | 3 | 邢科長(zhǎng) | 1 |
| 9 | 4 | 范組長(zhǎng) | 2 |
| 10 | 4 | 趙組長(zhǎng) | 2 |
| 11 | 4 | 姬組長(zhǎng) | 1 |
+----+---------+-----------+------+
11 rows in set (0.00 sec)
基本業(yè)務(wù)
簡(jiǎn)單信息報(bào)表: 查詢用戶信息
mysql> SELECT
-> id,
-> user_name AS '姓名',
-> ( CASE WHEN sex = 1 THEN '男' WHEN sex = 2 THEN '女' ELSE '未知' END ) AS '性別'
-> FROM
-> USER;
+----+-----------+--------+
| id | 姓名 | 性別 |
+----+-----------+--------+
| 1 | admin | 男 |
| 2 | 王經(jīng)理 | 男 |
| 3 | 李經(jīng)理 | 女 |
| 4 | 張經(jīng)理 | 女 |
| 5 | 王科長(zhǎng) | 男 |
| 6 | 李科長(zhǎng) | 男 |
| 7 | 呂科長(zhǎng) | 女 |
| 8 | 邢科長(zhǎng) | 男 |
| 9 | 范組長(zhǎng) | 女 |
| 10 | 趙組長(zhǎng) | 女 |
| 11 | 姬組長(zhǎng) | 男 |
+----+-----------+--------+
查詢每個(gè)角色名稱及對(duì)應(yīng)人員中女性數(shù)量
mysql> SELECT
-> r.id,
-> r.role_name AS role,
-> count( u.sex ) AS sex
-> FROM
-> role r
-> LEFT JOIN USER u ON r.id = u.role_id
-> AND u.sex = 2
-> GROUP BY
-> r.role_name
-> ORDER BY
-> r.id ASC;
+----+-----------+-----+
| id | role | sex |
+----+-----------+-----+
| 1 | 管理員 | 0 |
| 2 | 總經(jīng)理 | 2 |
| 3 | 科長(zhǎng) | 1 |
| 4 | 組長(zhǎng) | 2 |
+----+-----------+-----+
4 rows in set (0.00 sec)
假如我們把性別過濾的條件改為 where 操作結(jié)果會(huì)怎么樣呢?
mysql> SELECT
-> r.id,
-> r.role_name AS role,
-> count( u.sex ) AS sex
-> FROM
-> role r
-> LEFT JOIN USER u ON r.id = u.role_id
-> WHERE
-> u.sex = 2
-> GROUP BY
-> r.role_name
-> ORDER BY
-> r.id ASC;
+----+-----------+-----+
| id | role | sex |
+----+-----------+-----+
| 2 | 總經(jīng)理 | 2 |
| 3 | 科長(zhǎng) | 1 |
| 4 | 組長(zhǎng) | 2 |
+----+-----------+-----+
3 rows in set (0.00 sec)
這里可以看到角色數(shù)據(jù)不完整了。
找出角色為總經(jīng)理的員工數(shù)量
mysql> SELECT
-> r.id,
-> r.role_name AS role,
-> count( u.sex ) AS sex
-> FROM
-> role r
-> LEFT JOIN USER u ON r.id = u.role_id
-> WHERE
-> r.role_name = '總經(jīng)理'
-> GROUP BY
-> r.role_name
-> ORDER BY
-> r.id ASC;
+----+-----------+-----+
| id | role | sex |
+----+-----------+-----+
| 2 | 總經(jīng)理 | 3 |
+----+-----------+-----+
1 row in set (0.00 sec)
同樣將過濾條件由 where 改為 on
mysql> SELECT
-> r.id,
-> r.role_name AS role,
-> count( u.sex ) AS sex
-> FROM
-> role r
-> LEFT JOIN USER u ON r.id = u.role_id
-> AND r.role_name = '總經(jīng)理'
-> GROUP BY
-> r.role_name
-> ORDER BY
-> r.id ASC;
+----+-----------+-----+
| id | role | sex |
+----+-----------+-----+
| 1 | 管理員 | 0 |
| 2 | 總經(jīng)理 | 3 |
| 3 | 科長(zhǎng) | 0 |
| 4 | 組長(zhǎng) | 0 |
+----+-----------+-----+
4 rows in set (0.00 sec)
這里可以看到數(shù)據(jù)多余了
總結(jié)
在 left join 語(yǔ)句中,左表過濾必須放 where 條件中,右表過濾必須放 on 條件中,這樣結(jié)果才能不多不少,剛剛好。
到此這篇關(guān)于MySQL聯(lián)表查詢基本操作之left-join常見坑的文章就介紹到這了,更多相關(guān)MySQL聯(lián)表查詢left-join內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- MYSQL使用inner join 進(jìn)行 查詢/刪除/修改示例
- MySQL查詢優(yōu)化:連接查詢排序limit(join、order by、limit語(yǔ)句)介紹
- MySQL優(yōu)化之使用連接(join)代替子查詢
- mysql多個(gè)left join連接查詢用法分析
- mysql使用from與join兩表查詢的區(qū)別總結(jié)
- MySQL中(JOIN/ORDER BY)語(yǔ)句的查詢過程及優(yōu)化方法
- mysql中各種常見join連表查詢實(shí)例總結(jié)
- MySQL利用profile分析慢sql詳解(group left join效率高于子查詢)
- Mysql連接join查詢?cè)碇R(shí)點(diǎn)
- mysql高效查詢left join和group by(加索引)