mysqlleftjoin合并_SQLleftjoin左表合并去重技巧总结建表:
CREATE TABLE `table1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(60) DEFAULT NULL,
`age` varchar(200) DEFAULT NULL,
`sponsor_id` varchar(20) DEFAULT NULL COMMENT '业务发起⼈',
`gmt_create_user` int(11) NOT NULL COMMENT '创建⼈id',
`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`gmt_modified` datetime DEFAULT NULL COMMENT '修改时间',
`gmt_modified_user` int(11) DEFAULT NULL COMMENT '修改⼈id',
PRIMARY KEY (`id`)
)
ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COMMENT='测试表1';
CREATE TABLE `table2` (
`kid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(60) DEFAULT NULL,
sql left join 多表连接`sponsor_id` varchar(20) DEFAULT NULL COMMENT '业务发起⼈',
`type` int(11) NOT NULL COMMENT '创建⼈id',
`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`gmt_modified` datetime DEFAULT NULL COMMENT '修改时间',
`gmt_modified_user` int(11) DEFAULT NULL COMMENT '修改⼈id',
PRIMARY KEY (`kid`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4 COMMENT='测试表2';
插⼊数据:
INSERT INTO `table1`(`id`, `name`, `age`, `sponsor_id`, `gmt_create_user`, `gmt_create`, `gmt_modified`,
`gmt_modified_user`) VALUES (1, 't1', '11', '10', 1, '2018-10-10 20:34:03', NULL, NULL);
INSERT INTO `table1`(`id`, `name`, `age`, `sponsor_id`, `gmt_create_user`, `gmt_create`, `gmt_modified`,
`gmt_modified_user`) VALUES (2, 't2', '12', '10', 2, '2018-10-10 20:34:03', NULL, NULL);
INSERT INTO `table1`(`id`, `name`, `age`, `sponsor_id`, `gmt_create_user`, `gmt_create`, `gmt_modified`,
`gmt_modified_user`) VALUES (3, 't3', '13', '10', 3, '2018-10-10 20:34:03', NULL, NULL);
INSERT INTO `table1`(`id`, `name`, `age`, `sponsor_id`, `gmt_create_user`, `gmt_create`, `gmt_modified`,
`gmt_modified_user`) VALUES (4, 't4', '14', '20', 4, '2018-10-10 20:34:03', NULL, NULL);
INSERT INTO `table2`(`kid`, `name`, `sponsor_id`, `type`, `gmt_create`, `gmt_modified`, `gmt_modified_user`) VALUES (1, 't1', '10', 1, '2018-10-10 20:38:10', NULL, NULL);
INSERT INTO `table2`(`kid`, `name`, `sponsor_id`, `type`, `gmt_create`, `gmt_modified`, `gmt_modified_user`) VALUES (2, 't2', '10', 1, '2018-10-10 20:38:10', NULL, NULL);
INSERT INTO `table2`(`kid`, `name`, `sponsor_id`, `type`, `gmt_create`, `gmt_modified`, `gmt_modified_user`) VALUES (3, 't3', '10', 1, '2018-10-10 20:38:10', NULL, NULL);
INSERT INTO `table2`(`kid`, `name`, `sponsor_id`, `type`, `gmt_create`, `gmt_modified`, `gmt_modified_user`) VALUES (4, 't4', '10', 1, '2018-10-10 20:38:10', NULL, NULL);
INSERT INTO `table2`(`kid`, `name`, `sponsor_id`, `type`, `gmt_create`, `gmt_modified`, `gmt_modified_user`) VALUES (5, 't5', '10', 1, '2018-10-10 20:38:10', NULL, NULL);
INSERT INTO `table2`(`kid`, `name`, `sponsor_id`, `type`, `gmt_create`, `gmt_modified`, `gmt_modified_user`) VALUES (6, 't6', '10', 1, '2018-10-10 20:38:10', NULL, NULL);
INSERT INTO `table2`(`kid`, `name`, `sponsor_id`, `type`, `gmt_create`, `gmt_modified`, `gmt_modified_user`) VALUES (7, 't7', '10', 2, '2018-10-10 20:38:10', NULL, NULL);
INSERT INTO `table2`(`kid`, `name`, `sponsor_id`, `type`, `gmt_create`, `gmt_modified`, `gmt_modified_user`) VALUES (8, 't1', '11', 1, '2018-10-10 20:38:10', NULL, NULL);
查询异常:
SELECT
a.*,
FROM
table1 a
LEFT JOIN table2 b ON a.sponsor_id = b.sponsor_id
WHERE
AND a.sponsor_id = 10;
简单说明问题出现的原因:
MySQL left join 语句格式为: A LEFT JOIN B ON 条件表达式
left join 是以A表为基础,A表即左表,B表即右表。
左表(A)的记录会全部显⽰,⽽右表(B)只会显⽰符合条件表达式的记录,如果在右表(B)中没有符合条件的记录,则记录不⾜的地⽅为NULL。
使⽤left join, A表与B表所显⽰的记录数为 1:1 或 1:0,A表的所有记录都会显⽰,B表只显⽰符合条件的记录。
但如果B表符合条件的记录数⼤于1条,就会出现1:n的情况,这样left join后的结果,记录数会多于A表的记录数。
所以解决办法 都是从⼀个出发点出发,使A表与B表所显⽰的记录数为 1:1对应关系。
解决⽅法:
使⽤⾮唯⼀标识的字段做关联
1 DISTINCT
select DISTINCT(id) from a left join b on a.id=b.aid DISTINCT查询结果是 第⼀个表唯⼀的数据 重复的结果没显⽰出来
SELECT
DISTINCT(a.id), a.*,
FROM
table1 a
LEFT JOIN table2 b ON a.sponsor_id = b.sponsor_id
WHERE
AND a.sponsor_id = 10;
SELECT
DISTINCT a.*,
FROM
table1 a
LEFT JOIN table2 b ON a.sponsor_id = b.sponsor_id
WHERE
AND a.sponsor_id = 10;
2 GROUP BY
select * from a left join(select id from b group by id) as b on a.id=b.aid 拿出b表的⼀条数据关联 使A表与B表所显⽰的记录数为 1:1对应关系。
SELECT
a.*,
FROM
table1 a
LEFT JOIN ( SELECT * FROM table2 GROUP BY sponsor_id ) AS b ON a.sponsor_id = b.sponsor_id
WHERE
AND a.sponsor_id = 10;
3 max取唯⼀
select * from a left join (select max(id) from table group by id) as b on a.id=b.aid 拿出b表的最后⼀条数据关联
SELECT
a.*,
FROM
table1 a
LEFT JOIN ( SELECT MAX( kid ), type, sponsor_id FROM table2 GROUP BY sponsor_id ) AS b ON a.sponsor_id =
b.sponsor_id
WHERE
AND a.sponsor_id = 10;
4 IN巧⽤
SELECT
a.*
FROM
table1 a
WHERE
a.sponsor_id IN ( SELECT sponsor_id FROM table2 WHERE type = 1 AND sponsor_id = 10 );
SELECT
a.*,
1
FROM
table1 a
WHERE
a.sponsor_id IN ( SELECT sponsor_id FROM table2 WHERE type = 1 AND sponsor_id = 10 );
相信对于熟悉SQL的⼈来说,LEFT JOIN⾮常简单,采⽤的时候也很多,但是有个问题还是需要注意⼀下。假如⼀个主表M有多个从表的话A B C …..的话,并且每个表都有筛选条件,那么把筛选条件放到哪⾥,就得注意喽。
⽐如有个主表M,卡号是主键。
卡号
客户号622312**********
1001
622312**********
1002
622312**********
1003
有个从表A,客户号、联系⽅式是联合主键,其中联系⽅式,1-座机,2-⼿机号码
客户号
联系⽅式
联系号码1001
1
010-78586
1001
2
188********

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。