简体   繁体   English

sql两个列的一个表引用另一个表中的同一列

[英]sql two columns of one table reference same column in another table

I have these 2 tables 我有这两张桌子

tbl_link

    pID | fID_a | fID_b | link_desc
     1  |  1    |  2    |  aa + bb

tbl_structure

    pID | desc
     1  |  a
     2  |  b

fID_a and fID_b are foreign keys to pID in tbl_structure fID_a doesn't allow NULL values while fID_b does fID_a和fID_b是tbl_structure中pID的外键fID_a不允许NULL值,而fID_b执行

I am trying to retrieve the desc of structure 2 when querying for structure 1 我在查询结构1时尝试检索结构2的desc

my sql query at the moment looks like this 我的SQL查询此刻看起来像这样

SELECT a.link_desc, tbl_structure.desc FROM tbl_strukture 
LEFT JOIN tbl_link as a ON tbl_structure.pID = a.fID_a 
LEFT JOIN tbl_link as b ON tbl_structure.pID = b.fID_b 
WHERE tbl_structure.pID = 1

but I only get the desc of the structure with pID 1! 但我只得到pID 1结构的desc!

Thanks for your help! 谢谢你的帮助!

Are you looking for this? 你在找这个吗?

SELECT l.link_desc, 
       s.[desc] description1,
       s2.[desc] description2
  FROM tbl_link l LEFT JOIN tbl_structure s 
    ON l.fID_a = s.pID LEFT JOIN tbl_structure s2 
    ON l.fID_b = s2.pID 
WHERE s.pID = 1

SQLFiddle (SQL Server) SQLFiddle (SQL Server)

SQLFiddle (MySql) SQLFiddle (MySql)

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

相关问题 将一个表中的两列连接到另一引用表中的列 - Join two columns in one table to a column in another reference table 将一个表的两列连接到另一表的同一列 - Join two columns of one table to the same column of another table sql一个表的两列引用另一表的同一列 - sql Two columns of one table references to the same column of the other table SQL连接-一列用作另一表中两列的ID - SQL join - one column serving as ID for two columns in another table 将一个sql列基于两个列求和,将另一个表与条件求和 - sum one sql column based on two columns another table with criteria 从一个表中获取一列,而从另一表中获取两列 - Get one column from one table with two columns of another table 从一个表中的两个不同列中选择数据,这些列指向另一个表中的同一列 - selecting data from two different columns in one table that point to the same column in another table 一个表中的两列是否可以具有另一表中同一列的外键? - Can two columns from one table have a foreign key to the same column in another table? 如何将SQL表中的2列连接到另一个SQL表中的一列? - How to join 2 columns in a SQL table to one column in another SQL table? SQL查询联接一列的平均值,其中另一列在另一表的两列值之间 - Sql query join average of one column where another column is between two columns values of another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM