简体   繁体   English

SQL 两个外键链接到另一个表的一个主键以从该表中提取字段

[英]SQL Two foreign keys linked to one primary key of another table to pull field from that table

The question title is probably confusing so I'll try to clear it up.问题标题可能令人困惑,所以我会尝试清除它。

I have two columns in a grid.我在网格中有两列。 State and Locality. State 和位置。 I have two foreign keys in a table.我在一个表中有两个外键。 Notification.fkState and Notification.fkLocality. Notification.fkState 和 Notification.fkLocality。 They are both linked to the primary key of another table.它们都链接到另一个表的主键。 Location.pkLocation I want the ALIASES of State and Locality to equal the Location.Name from the Location Table according to the foreign key value linked the primary key pkLocation. Location.pkLocation 我希望 State 和 Locality 的 ALIASES 根据链接主键 pkLocation 的外键值等于位置表中的 Location.Name。

Is there a statement like有没有类似的声明

SELECT Location.Name(WHERE Notification.fkState=Location.pkLocation) AS State, Location.Name(WHERE Notification.fkLocality=Location.pkLocation) AS Locality FROM Notification INNER JOIN Locality ON Notification.fkState=Location.pkLocation AND Notification.fkLocality=Location.pkLocation

My AND is currently an OR because when it is AND...it displays nothing.我的 AND 目前是 OR,因为当它是 AND 时……它什么也不显示。

Any help?有什么帮助吗?

SELECT S.Name AS Locality, N.Name AS State
From Notification N
Inner Join Location S on N.fkstate=S.pkLocation
Inner Join Location L on  N.fkLocality=L.pkLocation

暂无
暂无

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

相关问题 如何创建具有两个主键的表,其中一个是一个表中的外键,另一个是另一个表中的外键? - how to create a table with two primary keys where one is foreign key in one table and the other on another table? 将一个表中的外键数组与另一表中的主键匹配? - Match an array of foreign keys in one table with primary key in another table? SQL中的表能否将多列作为仅引用另一表的一个主键的外键? - Can a table in SQL have multiple columns as foreign keys that refer only to one primary key of another table? 两个外键引用另一个表的主键 - Two foreign keys reference the primary key of another table Java/Mysql 从一个表中选择主键值并将它们作为外键插入到另一个表中 - Java/Mysql Selecting Primary Key values from one table and Insert them to another table as Foreign Keys 从一个表到另一个表中的单个主键有多个外键是否可以? - Is it fine to have multiple foreign keys from one table to single primary key in another table? 从表中使用多个外键选择一个主键 - 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 中,如何将一个表的多个列中的多个外键 ID 连接到另一个表中的单个主键和唯一的 select 一个? - In SQL, how to join multiple foreign keys IDs in multiple columns of a table to a single primary key in another table and uniquely select one? 显示与一个主键链接的两个外键 - Displaying two foreign keys linked with one primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM