如何使用sql server加入4表来获取所有4个表数据

时间:2017-08-31 04:29:32

标签: sql-server sql-server-2008

我有下表

TABLE_1

class Game:
    def __init__(self):
        self.mailbox_full = True

    def take_leaflet(self):
        if self.location != FRONT_HOUSE:
            self.bogus()
            return

        if self.mailbox_full:
            self.inventory.add_item(Item('Leaflet', 0, 0))
            self.mailbox_full = False
            self.msg('You take the leaflet')
        else:
            self.msg('There is no leaflet in the mailbox.')

TABLE_2

id  Name     City
1   Mazhar   Banglore

TABLE_3

id  College Name      City
1   R K           Banglore

TABLE_4

id  Passport No      Country
1   A5222           India

我需要如下所示的结果,所有表都有基于id的公共id需要结果。

id  Current Employe      Experience
1     XYZ                   2

如果我将来有更多的桌子也可以加入额外的桌子

1 个答案:

答案 0 :(得分:1)

非常简单的加入!试试这个:

SELECT Table_1.id,
       Table_1.Name,
       Table_1.City,
       Table_2.[College Name],
       Table_2.City,
       Table_3.[Passport No],
       Table_3.Country,
       Table_4.[Current Employe],
       Table_4.Experience
 FROM Table_1
    INNER JOIN Table_2 ON Table_2.id = Table_1.id
    INNER JOIN Table_3 ON Table_3.id = Table_1.id
    INNER JOIN Table_4 ON Table_4.id = Table_1.id