简体   繁体   English

SQL Server:从第一个表链接到第二个表的主键的多个外键

[英]SQL Server : multiple foreign keys from first table linking to the primary key of second table

I have two tables, itemrequest_table and item_table . 我有两个表, itemrequest_tableitem_table Four columns of itemrequest_table ( item1, item2, item3, item4 ) are connected to item_no of item_table . itemrequest_table四列( item1, item2, item3, item4 )连接到item_noitem_table

I'm trying to write a query to display the item names of item_nos under the column of item1, item2, item3, item4. 我正在尝试编写查询以在item1,ite2,ite3,item4列下显示item_nos的项目名称。

But how can I display the item names it? 但是如何显示项目名称呢? And is it possible to query that? 可以查询吗?

My tables: 我的桌子:

在此处输入图片说明

You just need to do four joins between the two tables - one for each item: 您只需要在两个表之间进行四个联接-每个项一个:

SELECT
   Item1 = i1.item_name,    
   Item2 = i2.item_name,
   Item3 = i3.item_name,
   Item4 = i4.item_name
FROM
   itemrequest_table ir
INNER JOIN
   item_table i1 ON ir.item1 = i1.item_no
INNER JOIN
   item_table i2 ON ir.item2 = i2.item_no
INNER JOI
   item_table i3 ON ir.item3 = i3.item_no
INNER JOIN
   item_table i4 ON ir.item4 = i4.item_no       

But this is really a bad design - it smells of first-normal form violation ( repeating columns ). 但这确实是一个糟糕的设计-闻到第一范式违规( 重复列 )的味道。 What you really should do is just have a relationship between itemrequest_table and the item_table that allows for any number of connections, if needed (a regular, normal m:n relationship) 真正应该做的就是在itemrequest_tableitem_table之间itemrequest_table一个关系,该关系允许任意数量的连接(如果需要的话)(常规的正常m:n关系)

You need to perform four joins: 您需要执行四个联接:

SELECT    ir.request_no, 
          t1.item_name, t2.item_name, t3.item_name, t4.item_name
FROM      itemrequest_table ir
LEFT JOIN item_table t1 ON ir.item1 = t1.item_no
LEFT JOIN item_table t2 ON ir.item2 = t2.item_no
LEFT JOIN item_table t3 ON ir.item3 = t3.item_no
LEFT JOIN item_table t4 ON ir.item4 = t4.item_no

EDIT: 编辑:
As per the comments, these items are optional, so a left join should be used instead of a join . 根据注释,这些项目是可选的,因此应使用left join代替join

暂无
暂无

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

相关问题 一个表中的多个外键链接到第二个表中的单个主键 - Multiple foreign keys from one table linking to single primary key in second table 具有链接到同一主键的多个外键的表 - Table with multiple foreign keys linking to the same primary key 从表中使用多个外键选择一个主键 - Select one Primary Key using multiple Foreign Keys from a table SQL-从三个表中选择数据,其中一个表具有多个到同一个主键的外键 - SQL - Select data from three tables where one table has multiple foreign keys to the same primary key 更改Sql表(将外键更改为表的第二主键) - Alter Sql table (Change foreign key to Second primary of the table) SQL中的表能否将多列作为仅引用另一表的一个主键的外键? - Can a table in SQL have multiple columns as foreign keys that refer only to one primary key of another table? SQL 两个外键链接到另一个表的一个主键以从该表中提取字段 - SQL Two foreign keys linked to one primary key of another table to pull field from that table 从一个表到另一个表中的单个主键有多个外键是否可以? - Is it fine to have multiple foreign keys from one table to single primary key in another table? SQL Server:引用表中没有与外键'FK'中的引用列列表匹配的主键或候选键 - SQL Server : there are no primary or candidate keys in the referenced table that match the referencing column list in the foreign key 'FK' mysql表中的多个外键指向相同的主键 - mysql Multiple Foreign Keys in a Table to the Same Primary Key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM