内部连接的简单示例在4个表之间 [英] Simple Examples for Innerjoin betweeen 4 tables

查看:70
本文介绍了内部连接的简单示例在4个表之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在3或4个表之间查询内部连接的示例。

解决方案

连接表并不是什么大问题。尝试下面给出的例子:

示例:假设您有4个表(Table1,Table2,Table3,Table4)。

   -    Table1  
ID EmployeeID
- - -------------------
1 S001
2 S002

- 表2
ID名称
- ----------------
1 Amit
2 Amy

- 表3
ID地址
- ------------ ----
1 禁令galore
2 Gaya

- 表4
ID PhoneNo
- ---- ------------
1 0123456789
2 9876543210





现在,如果你想将这三个表连接到相应ID的数据,你可以尝试这个sql:

  SELECT  A.EmployeeID,B.Name,C.Address,D.PhoneNo 
FROM 表1 INNER JOIN 表2 B A.ID = B.ID
INNER JOIN 表3 C A.ID = C.ID
INNER JOIN 表4 D on A.ID = D.ID
WHERE A.ID = 101 - 您可以添加任何条件



有关详细信息,请参阅以下链接:

MSDN:加入三个或更多表 [ ^ ]

SQL Server 2008 - 如何加入3个表 [ ^ ]





- 阿米特

示例:...

  SELECT  
employee.EmployeeId,
employee.Employeename,
enduser.username,
exedistributornw.HeadQuaters,
party.PartyName
FROM
员工
内部 加入 enduser ON employee.EmployeeId = enduser.employeeid
内部 加入 exedistributornw ON employee.EmployeeId = exedistributornw.EmployeeId
内部 加入 party ON exedistributornw.PartyId = party.PartyId


I need the examples query for innerjoin between the 3 or 4 tables.

解决方案

Joining tables is not a big deal. Try the given example below:
Example : Suppose you are having 4 tables(Table1, Table2, Table3, Table4).

--Table1
ID        EmployeeID
---------------------
1         S001
2         S002

--Table2
ID        Name
------------------
1         Amit
2         Amy

--Table3
ID        Address
------------------
1         Bangalore
2         Gaya

--Table4
ID        PhoneNo
------------------
1         0123456789
2         9876543210



Now, if you want to join these three tables for data of corresponding ID, you can try this sql:

SELECT A.EmployeeID, B.Name, C.Address, D.PhoneNo
    FROM Table1 A INNER JOIN Table2 B on A.ID = B.ID
    INNER JOIN Table3 C on A.ID = C.ID
    INNER JOIN Table4 D on A.ID = D.ID
    WHERE A.ID = 101 -- You can add any conditions


Refer the links below for more details:
MSDN : Joining Three or More Tables[^]
SQL Server 2008 - How to Join 3 tables[^]


--Amit


Example:...

SELECT
employee.EmployeeId,
employee.Employeename,
enduser.username,
exedistributornw.HeadQuaters,
party.PartyName
FROM
employee
Inner Join enduser ON employee.EmployeeId = enduser.employeeid
Inner Join exedistributornw ON employee.EmployeeId = exedistributornw.EmployeeId
Inner Join party ON exedistributornw.PartyId = party.PartyId


这篇关于内部连接的简单示例在4个表之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆