加入多个一对多表没有重复行

时间:2015-01-21 23:40:16

标签: mysql

我正在尝试从[一对一]表中连接两个[一对多]表,而查询工作时会产生重复或更多行。请帮忙,因为我迫不及待地想要这个工作。这是简单格式的查询...

SELECT a.id, b.*, c.*
FROM tableA a 
LEFT JOIN tableB b
   ON a.id = b.id
LEFT JOIN tableC c
   ON a.id = c.id
WHERE a.id = 12345

1 个答案:

答案 0 :(得分:0)

如果distinct不起作用,请尝试为表和表C创建两个单独的视图,然后对这两个视图进行连接:

Create view view_tableB as
SELECT a.id, b.*
FROM tableA a 
LEFT JOIN tableB b
   ON a.id = b.id
WHERE a.id = 12345;


Create view view_tableC as
SELECT a.id, c.*
FROM tableA a 
LEFT JOIN tableC c
   ON a.id = c.id
WHERE a.id = 12345;


Select * from view_tableB join view_tableC;