mysql,left join查询,数据变多了????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql,left join查询,数据变多了????相关的知识,希望对你有一定的参考价值。
A表有3条数据,B有10条数据,
select a.* from A
left join B on a.id=b.id
为什么查出来的数据是大于3条啊,
比如B表中有一个外键是参考A表的,假设字段为a_id,那连接条件应该是
a.id = b.a_id,这样数据才正常 参考技术A 左连接是从左表那里返回所有的行,即使在右表中没有匹配的行,如果有多个匹配就会返回多个,所以数据没有变多。本回答被提问者采纳 参考技术B 你后面加上where b.id is null试试 参考技术C 因为b.id有重复的匹配了a.id 有一个重复对上了 就会多一条数据
Postgresql left join左连接后数据变多去重记录(当左表1对多右表时)
Postgresql left join左连接后数据变多去重记录(当左表1对多右表时)
1. 构造表数据
drop table if exists t_cover;
create table if not exists t_cover(
pname text NOT NULL,
upload_date varchar(12),
city_code varchar(20) default '',
total_dis varchar(20),
create_time timestamp with time zone NOT NULL DEFAULT now(),
constraint t_cover_unique_key unique (pname,upload_date,city_code)
);
COMMENT ON TABLE t_cover IS '统计中间表';
COMMENT ON COLUMN t_cover.pname IS '名称';
COMMENT ON COLUMN t_cover.upload_date IS '日期';
COMMENT ON COLUMN t_cover.city_code IS '城市行政编码';
COMMENT ON COLUMN t_cover.total_dis IS '里程';
COMMENT ON COLUMN t_cover.create_time IS '创建时间';
create index if not exists t_cover_citycode on t_cover(city_code);
create index if not exists t_cover_pname on t_cover(pname);
create index if not exists t_cover_uploaddate on t_cover(upload_date);
INSERT INTO public.t_cover(pname, upload_date, city_code, total_dis) VALUES ('aa', '202201', '110099', 99.88);
INSERT INTO public.t_cover(pname, upload_date, city_code, total_dis) VALUES ('aa', '202201', '110000', 64.96);
INSERT INTO public.t_cover(pname, upload_date, city_code, total_dis) VALUES ('aa', '202201', '120000', 82.77);
INSERT INTO public.t_cover(pname, upload_date, city_code, total_dis) VALUES ('aa', '202202', '110108', 23.63);
INSERT INTO public.t_cover(pname, upload_date, city_code, total_dis) VALUES ('aa', '202202', '120000', 17.97);
DROP TABLE if exists t_county;
CREATE TABLE if not exists t_county
(
province_code integer,
province_name character varying(32),
city_code integer,
city_name character varying(32),
county_code integer,
county_name character varying(32)
);
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110114,'昌平区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110115,'大兴区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110101,'东城区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110111,'房山区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110106,'丰台区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110108,'海淀区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110116,'怀柔区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110109,'门头沟区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110118,'密云区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110117,'平谷区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110107,'石景山区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110113,'顺义区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110112,'通州区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110102,'西城区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110119,'延庆区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (110000,'北京市',110099,'北京市',110105,'朝阳区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120115,'宝坻区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120113,'北辰区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120116,'滨海新区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120110,'东丽区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120101,'和平区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120105,'河北区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120102,'河东区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120103,'河西区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120106,'红桥区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120119,'蓟州区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120112,'津南区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120118,'静海区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120104,'南开区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120117,'宁河区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120114,'武清区');
INSERT INTO t_county(province_code, province_name, city_code, city_name, county_code, county_name) VALUES (120000,'天津市',120099,'天津市',120111,'西青区');
2. 查询验证
2.1 单独查询左表
2.2 left join查询:左连接,左表1对多右表 n多条数据明显有重复
2.3 左连接,左表1对多右表 n多条数据明显有重复 去重(根据左表唯一键)
bingo,成功~
以上是关于mysql,left join查询,数据变多了????的主要内容,如果未能解决你的问题,请参考以下文章
Mysql 连接(left join, right join, inner join ,full join)