简体   繁体   English

根据从另一表中选择的结果从一个表中选择

[英]Select from one table based on results of select from another table

I'm trying to return rows from one table, based on the results of a select from another table, but I'm not well versed in SQL/Oracle and am struggling to link the two together. 我试图根据另一个表的选择结果返回一个表中的行,但是我对SQL / Oracle并不熟悉,并且很难将两者链接在一起。

ITEMS                 DEFS
item_id (1000)        trans (1125)
item_state (Y)        desp (680)
trans (1125)
orig_trans (1100)

Overall, I'm trying to query the defs.desp number, and return items.item_id, items.item_state and items.orig_trans, but I can't tell if I need a join or a subquery (or if what I'm trying to achieve is even possible in the first place!) 总的来说,我正在尝试查询defs.desp编号,并返回items.item_id,item.item_state和items.orig_trans,但我无法确定我是否需要连接或子查询(或者是否正在尝试甚至有可能实现!)

What I'd like is something like: 我想要的是这样的:

SELECT I.ITEM_ID, I.ITEM_STATE, I.ORIG_TRANS
FROM ITEMS I
JOIN DEFS DF
ON I.TRANS = (*FIND 'TRANS' FROM DEFS TABLE USING THE DESP NUMBER BELOW*)
WHERE DF.DESP = 680

And the final desired output being: 最终所需的输出是:

ITEM_ID    ITEM_STATE    ORIG_TRANS
1000       Y             1100

The order in which things are returned is what's throwing me, since my final output needs to be from the ITEMS table, but the first thing I need to find out is the TRANS number from the DEFS table? 因为我的最终输出需要来自ITEMS表,所以返回结果的顺序让我感到困惑,但是我首先要了解的是DEFS表中的TRANS编号?

Isn't it what you need? 这不是您所需要的吗?

SELECT I.ITEM_ID, I.ITEM_STATE, I.ORIG_TRANS
FROM ITEMS I
JOIN DEFS DF
ON I.TRANS = DF.TRANS
WHERE DF.DESP = 680

Try: 尝试:

SELECT I.ITEM_ID, I.ITEM_STATE, I.ORIG_TRANS
FROM ITEMS I
JOIN (
SELECT TRANS FROM DEFS WHERE DESP = 680
) DF
ON I.TRANS = DF.TRANS

You can try below SQL 你可以尝试下面的SQL

SELECT ITEM_ID, ITEM_STATE, ORIG_TRANS FROM ITEMS WHERE TRANS IN ( SELECT TRANS FROM DEFS WHERE DESP = 680 ); 从要转移的项目中选择ITEM_ID,ITEM_STATE,ORIG_TRANS(从DEFS中的选择转移中DESP = 680);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM