通过映射表选择值。

时间:2013-06-10 18:58:24

标签: mysql sql-server linq tsql

我有三个表 senderTable recieverTable 和一个 mapTable 。我正在尝试编写查询以获取 senderName recieverName

的输出

senderTable

senderID
senderName

recieverTable

recieverID
recieverName

mapTable

mappingID
senderID
recieverID

我有以下查询无效,因为我不知道如何在其中合并映射表。

SELECT t_senderTable.nameFull AS "senderName", t_recieverTable.recieverName AS "recieverName" 
FROM ((dbo.t_senderTable AS t_senderTable
     INNER JOIN t_senderTable AS t_senderTable ON (t_senderTable.Kd = mapTable.senderID))
     INNER JOIN t_recieverTable AS t_recieverTabler ON (recieverTable.Id = mapTable.recieverID ))

任何想法都很好吗?谢谢

3 个答案:

答案 0 :(得分:0)

我不知道你的意思是正确的,如果它不起作用或以任何方式看看这个:

   SELECT s.nameFull AS senderName, r.recieverName AS recieverName 
 FROM t_senderTable AS s
 INNER JOIN mapTable as m ON s.Kd = m.senderID
 INNER JOIN t_recieverTable  as r ON r.Id = m.recieverID

摆脱()并用内连接中的mapTable替换senderTable

答案 1 :(得分:0)

这是你在找什么?

SELECT t_senderTable.nameFull AS "senderName"
     , t_recieverTable.recieverName AS "recieverName" 
  FROM dbo.t_senderTable AS t_senderTable
 INNER JOIN mapTable
    ON t_senderTable.Id = mapTable.senderID
 INNER JOIN t_recieverTable AS t_recieverTabler
    ON mapTable.recieverID = recieverTable.Id

答案 2 :(得分:0)

使用它:

SELECT t_senderTable.nameFull AS senderName
     , t_recieverTable.recieverName AS recieverName
  FROM dbo.t_senderTable
 INNER JOIN mapTable
    ON t_senderTable.Id = mapTable.senderID
 INNER JOIN t_recieverTable
    ON mapTable.recieverID = recieverTable.Id