关于SQL语句连接的思考
CREATE TABLE `test1` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`grade` int(20) NOT NULL,
PRIMARY KEY (`id`)
);
insert into test1 (name, grade) values("a", 10);
insert into test1 (name, grade) values("a", 11);
insert into test1 (name, grade) values("a", 12);
CREATE TABLE `test2` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`sexy` int(20) NOT NULL,
PRIMARY KEY (`id`)
);
insert into test2 (name, sexy) values("a", 0);
insert into test2 (name, sexy) values("b", 1);
select * from test1 inner join test2 on test1.name = test2.name;
输出结果
+----+------+-------+----+------+------+
| id | name | grade | id | name | sexy |
+----+------+-------+----+------+------+
| 1 | a | 10 | 1 | a | 0 |
| 2 | a | 11 | 1 | a | 0 |
| 3 | a | 12 | 1 | a | 0 |
+----+------+-------+----+------+------+
发现了什么问题?
假设test2表中有年龄这一列,如果我需要取test1在test2表里那部分人的年龄和的话,必须对test1的name取唯一,否则就会重复计算,造成数据冗余。
select * from test1 left join test2 on test1.name = test2.name;
输出结果
+----+------+-------+------+------+------+
| id | name | grade | id | name | sexy |
+----+------+-------+------+------+------+
| 1 | a | 10 | 1 | a | 0 |
| 2 | a | 11 | 1 | a | 0 |
| 3 | a | 12 | 1 | a | 0 |
+----+------+-------+------+------+------+
左联同理,只有test2表中有一个name与test1表中的一致,因为以左表为主,生成的连接表中就会有三条test2部分的重复数据。
在做表连接之后的聚合计算尤其要注意这一点。
select * from test1 right join test2 on test1.name = test2.name;
输出结果
+------+------+-------+----+------+------+
| id | name | grade | id | name | sexy |
+------+------+-------+----+------+------+
| 1 | a | 10 | 1 | a | 0 |
| 2 | a | 11 | 1 | a | 0 |
| 3 | a | 12 | 1 | a | 0 |
| NULL | NULL | NULL | 2 | b | 1 |
+------+------+-------+----+------+------+
第一次写博客,纪念一下!2019-05-06于杭州。