选择FROM 3表

时间:2011-09-14 09:08:17

标签: mysql sql phpmyadmin

嗯,我认为这是一个简单的问题,但我找不到解决方案。

我有三张桌子:

表1

id(AS t1id)    Name  LASTNAME Value 

1              a     z         50

2              b     e         60 

3              c     k         70

4              d     u         60

表2

id   idTable1   Name(AS t2me)   Value(AS t2ve)

1     1          er             50

2     1          zx             150

3     2          zc             300

表3

id   idTable1   Name(AS t3me)   Value(AS t3ve)

1     2         erxc   50

2     2         zvvx   150

3     2         zcz    300

如何使用SQL

获得此结果
t1id    Name      LASTNAME   t2me    t2ve     t3me     t3ve

1       a         z           er      50      erdxc    150

1       a         z           zx      150           

2       b         e           zc      300      erxc    50

2       b         e                            zvvx    150

2       b         e                            zcz     300

这可能吗?如果不是我能做什么?

1 个答案:

答案 0 :(得分:1)

SELECT t1.id as t1id, t1.Name, t1.LASTNAME, 
    t2.Name as t2me, t2.Value as t2ve, t3.Name as t3me, 
    t3.Value as t3ve from Table1 t1 
LEFT JOIN Table2 t2 on t1.id = t2.idTable1 
LEFT JOIN Table3 on t3.idTable1 = t1.id