简体   繁体   English

mysql左连接具有一对多关系的多个表

[英]mysql left join multiple tables with one-to-many relation

I created a simple test case: 我创建了一个简单的测试用例:

CREATE TABLE `t1` (
  `id` int NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
)

CREATE TABLE `t2` (
  `id2` int NOT NULL AUTO_INCREMENT,
  `id1` int,
  PRIMARY KEY (`id2`)
)

CREATE TABLE `t3` (
  `id3` int NOT NULL AUTO_INCREMENT,
  `id1` int,
  PRIMARY KEY (`id3`)
)

insert into t1 (id) values (1);

insert into t2 (id1) values (1),(1);

insert into t3 (id1) values (1),(1),(1),(1);

I need to select all DISTINCT data from t1 left join t2 and DISTINCT data from t1 left join t3, returning a total of 6 rows ,1 x (2 [from t2] + 4 [from t3]) = 6, but beacause of the nature of this join I get 8 rows, 1 [from t1] x 2 [from t2] x 4 [from t3] = 8. 我需要从t1左连接t2选择所有DISTINCT数据,从t1左连接t3选择DISTINCT数据,返回总共6行,1 x(2 [来自t2] + 4 [来自t3])= 6,但是由于这种连接的性质是,我得到8行,即1 [从t1] x 2 [从t2] x 4 [从t3] = 8。

select * from t1 left join t2 on (t1.id = t2.id1);
2 rows in set (0.00 sec)

select * from t1 left join t3 on (t1.id = t3.id1);
4 rows in set (0.00 sec)

select * from t1 left join t2 on (t1.id = t2.id1) left join t3 on (t1.id = t3.id1);
8 rows in set (0.00 sec)

select * from t1 left join t2 on (t1.id = t2.id1) union select * from t1 left join t3 on (t1.id = t3.id1);
4 rows in set (0.00 sec)

What query should I use to get just the 6 rows I need, is it posible without subquery's or I need them (It will be more complicatet in the big query where I need this) ? 我应该使用哪种查询来获取我需要的6行,是否可以在没有子查询的情况下使用还是我需要它们(在需要此功能的大查询中,它将变得更加复杂)?
I need this for a big query where I allready get data from 8 tables, but I need to get data from 2 more to get all the data I need in just one single query, but when joining the 9th table, the returned data get's duplicated (the 9th table in this simple test case would be t3, and the 8th will be t2). 我需要一个大型查询,我已经准备从8个表中获取数据,但是我需要从2个以上的表中获取数据,以便仅通过一个查询即可获取所需的所有数据,但是当连接第9个表时,返回的数据将被复制(在这个简单测试用例中,第9个表将是t3,而第8个表将是t2)。

I hope someone could show me the right path to follow. 我希望有人可以向我展示正确的道路。
Thank you. 谢谢。

UPDATE SOLVED: I realy don't know how to do this test case in one select, but in my BIG query I solved it this way: beacause I used group_concat and group by, I did it by spliting a value in multipe group_concat(DISTINCT ... ) and concat all of them like this 解决的更新:我真的不知道如何在一个选择中做这个测试用例,但是在我的BIG查询中,我是这样解决的:因为我使用了group_concat和group by,所以我通过在multipe group_concat(DISTINCT中拆分一个值)来做到这一点。 ...)并像这样连接所有人

// instead of this
... group_concat(DISTINCT concat(val1, val2, val3)) ...
// I did this
concat(group_concat(DISTINCT val1,val2), group_concat(DISTINCT val1,val3)) ... 

so the distinct on small groups of value prevent all of those duplicates. 因此,价值小群体上的独特之处可以防止所有这些重复。

I think there is a small mistake in @nick rulez 's query. 我认为@nick Rulez的查询中有一个小错误。 If it is written like this it really returns 6 rows: 如果是这样写的,它实际上会返回6行:

(SELECT * FROM t1 LEFT JOIN t2 ON (t1.id = t2.id1))
    UNION ALL
(SELECT * FROM t1 LEFT JOIN t3 ON (t1.id = t3.id1))

I'm not sure if you're looking for at this solution 我不确定您是否正在寻找这种解决方案

select * from t1 left join t2 on (t1.id = t2.id1);
union all
select * from t1 left join t3 on (t1.id = t3.id1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM