简体   繁体   English

选择引用同一个主键表的两个外键的名称

[英]Select name of two foreign keys referring to same primary key table

I have an inventory transaction table Transaction_Log我有一个库存交易表Transaction_Log

The columns are log_id , to_warehouse , from_warehouse , both FOREIGN KEYs referencing to the warehouse table OWHS with columns whs_id , whs_name列是log_idto_warehousefrom_warehouse ,这两个 FOREIGN KEY 都引用仓库表OWHS其中列whs_idwhs_name

I need to output with these columns: log_id , to_warehouse_name , from_warehouse_name我需要输出这些列: log_idto_warehouse_namefrom_warehouse_name

Tried this试过这个

SELECT t.log_id, w1.whs_name AS to_warehouse_name, w2.whs_name from_warehouse_name
FROM OWHS w1, OWHS w2
 INNER JOIN Transaction_Log t ON w1.whs_id = t.to_warehouse AND w2.whs_id = t.from_warehouse

but it throws an error但它会引发错误

invalid table name: w1.whs_id无效的表名:w1.whs_id

I'm not sure whether the query is correct to output what I want, but the error already throws me off.我不确定查询是否正确输出我想要的内容,但错误已经让我失望了。

I use SAP HANA database, but I changed the column names a bit for this post.我使用 SAP HANA 数据库,但我为这篇文章稍微更改了列名。 I'm also returning to database programming after several years, so I apologize if this is a basic question, but my googling returns nothing close to what I want.几年后我也回到数据库编程,所以如果这是一个基本问题,我深表歉意,但我的谷歌搜索没有接近我想要的结果。

Never use commas in the FROM clause.切勿FROM子句中使用逗号。 Period.时期。 Write the query as:将查询写为:

SELECT t.log_id, w1.whs_name AS to_warehouse_name, w2.whs_name from_warehouse_name
FROM OWHS w1 JOIN
     Transaction_Log t
     ON w1.whs_id = t.to_warehouse JOIN
     OWHS w2
     ON w2.whs_id = t.from_warehouse;

Although you could also fix the problem by replacing the , with CROSS APPLY that just confuses the logic.尽管您可以通过将,替换为CROSS APPLY来解决问题,但这只会混淆逻辑。 You are looking for two simple JOIN s.您正在寻找两个简单的JOIN

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

相关问题 创建一个有 2 个主键和两个外键的表,引用 2 个具有相同约束名称的不同表 - Creating a table with 2 primary keys and two foreign keys referring to 2 different tables with same constraint name 同一主键选择语句MYSQL的两个外键 - two foreign keys to same primary key select statement MYSQL SQL SERVER 查询 select 项上的两个外键引用一个主键 - Two foreign keys referring to one primary key on SQL SERVER query select items 错误:外键引用来自两个不同表的主键 - Error: foreign key referring to primary keys from two different tables 引用同一张表中的主键的外键 - Foreign key referring to primary key in the same table Mysql 两个不同的主键使用相同的外键 - same foreign key for two different primary keys 引用同一个主键的两个外键 - Two foreign keys referencing the same primary key SQL-从三个表中选择数据,其中一个表具有多个到同一个主键的外键 - SQL - Select data from three tables where one table has multiple foreign keys to the same primary key 尝试从同一张表引用两个主键时如何解决外键约束? - How to fix foreign key constraint when trying to references two primary keys from the same table? 合法的? 两个外键引用相同的主键 - Legit? Two foreign keys referencing the same primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM