[英]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_id
, to_warehouse
, from_warehouse
,这两个 FOREIGN KEY 都引用仓库表OWHS
其中列whs_id
, whs_name
I need to output with these columns: log_id
, to_warehouse_name
, from_warehouse_name
我需要输出这些列:
log_id
, to_warehouse_name
, from_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.