简体   繁体   English

在 SQL 中加入具有相同列的同一个表

[英]Joining in same table with same column in SQL

I have 3 tables, that the one of them is information center table assumed table 1.我有 3 个表,其中一个是信息中心表假定表 1。

Table1 as (tank information)表1为(油箱信息)

-----------------------------------------
Prikey   TankName
-----------------------------------------
24       Tank1
25       Tank2

Table2 as (source)表2为(来源)

-----------------------------------------
 ID         Tank Source ID   Opotional
-----------------------------------------
 1           25               Source
 2           24               test
            etc

Table3 as (Destination)表 3 为(目的地)

---------------------------------------------------
Tank Destination ID     Opotional      ID Source
---------------------------------------------------
25                      Destination    1 
24                      Destination    2
etc

I would like to get the "tank name" in twice and display the "Source" and "destination" with match ID of 'Tank'.我想两次获得“坦克名称”,并显示“来源”和“目的地”,匹配 ID 为“坦克”。 So both of them having Tank ID that I use on one query.所以他们都有我在一个查询中使用的 Tank ID。 I have tried joined but is crashed.我曾尝试加入但崩溃了。 Is it possible?是否可以?

Hope the result is :希望结果是:

--------------------------------------------------------------------------------------------------
Source (tank Name)   |  Destination (Tank Name)  | opotional (source) | Opotional (Destination)
--------------------------------------------------------------------------------------------------

You just need to use aliases, Both on the tables that you join more than once (Table1) and on the fields that you return more than once (TankName, Optional).您只需要在多次加入的表 (Table1) 和多次返回的字段 (TankName, Optional) 上使用别名。

Something like:就像是:

select Source.TankName as SourceTank, Destination.TankName as DestinationTank, 
       Table2.Optional as SourceOptional, Table3.Optional as DestionationOptional
from Table2
     inner join Table3 on Table3.ID = Table2.ID
     inner join Table1 as Source on Source.PriKey = Table2.TankID
     inner join Table1 as Destination on Destination.PriKey = Table3.TankID
SELECT [Table2].Opotional AS 'opotional (source)',
       [Table3].Opotional AS 'Opotional (Destination)',
       [Table1].TankName AS 'Source (tank Name)',
       t1.TankName AS 'Destination (Tank Name)'
FROM [Table2]
     INNER JOIN [Table3] ON Table3.[ID Source] = [Table2].[ID]
     INNER JOIN Table1 ON [Table2].[Tank Source ID] = [Table1].Prikey
     INNER JOIN Table1 t1 ON Table3.[Tank Destination ID] = t1.Prikey;

This query will help you to achieve the expected result..此查询将帮助您达到预期的结果..

select a.Tankname as [Source (tank Name)],
       a.Tankname as[Destination (Tank Name)],
       b.Optional as [Optional Source],
       c.Optional as [Optional Destination] 
from Table2 as b 
    join Table3 as c on b.ID = c.[ID Source] 
    join Table1 as a on c.[Tank Destination ID] = a.prikey

Try this:尝试这个:

DECLARE @tbl1 as TABLE(
    Prikey  INT,
    TankName VARCHAR(50)
)

DECLARE @tbl2 as TABLE(
   ID  INT,
   TankSourceID  INT,
   Opotional  VARCHAR(50)
)

DECLARE @tbl3 as TABLE(
   TankDestinationID  INT,
   Opotional VARCHAR(50),
   IDSource  INT
)

INSERT INTO @tbl1 VALUES(1,'Tank1')
INSERT INTO @tbl1 VALUES(2,'Tank2')
INSERT INTO @tbl1 VALUES(3,'Tank3')

INSERT INTO @tbl2 VALUES(1,1,'Source')
INSERT INTO @tbl2 VALUES(2,3,'Test')

INSERT INTO @tbl3 VALUES(1,'Destination1',1)
INSERT INTO @tbl3 VALUES(2,'Destination2',2)


SELECT
    T1.TankName AS 'Source',
    T1_2.TankName AS 'Destination',
    T2.Opotional AS 'SourceOpotional',
    T3.Opotional AS 'DestinationOpotional'
FROM @tbl2 T2
LEFT JOIN @tbl1 T1 ON T1.Prikey=T2.TankSourceID
LEFT JOIN @tbl3 T3 ON T2.TankSourceID=T3.TankDestinationID
LEFT JOIN @tbl1 T1_2 ON T3.TankDestinationID=T1_2.Prikey

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

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