基于另一个表的SQL查询
我有这样的桌子:
表1:
fname | ID1 | FID |
---|---|---|
面包 | xyz | 18bread |
鱼 | ABC | 45 |
鱼面包_OLD | BNQ | 18bread_old |
bacon | tbg | 77bacon |
鸡蛋 | CGS | 99EGGS |
BANANA | BHG | BANANA18 |
表2:
FNAME | FID |
---|---|
BREAD | 18 |
FID BREAD 18 FIFE BARD 18 FISH | 45 |
BAECON | 77 |
鸡蛋 | 99 |
目前搜索了一个简单的搜索,表1要找到食物的ID如下:
SELECT ID1
FROM TABLE1
WHERE NAME IN NAME_LIST
因此,例如,当名称列表为:{'Bread','Fish)时,然后返回ABC XYZ。问题在于,这是一个较旧版本的具有相同ID的“ Bread_old”。 (18)
我需要更改此操作,因此现在搜索是基于食物的FID而不是名称进行的,以找到受影响的食物,但我无法更改输入。
例如:给定食物列表:(“面包”,“鱼”)
结果应该是XYZ,ABC,BNQ(因为面包与表1中的BNQ相匹配,在表1中与BNQ匹配)
如何可以做到这一点?我认为我需要使用JOIN或进行“选择中的选择”,但我不确定如何与多个输入一起使用。
编辑:oracle是数据库 编辑2:将Banana18添加到表1中,需要领先匹配
I have tables like this:
table 1:
FNAME | ID1 | FID |
---|---|---|
BREAD | XYZ | 18BREAD |
FISH | ABC | 45FISH |
BREAD_OLD | BNQ | 18BREAD_OLD |
BACON | TBG | 77BACON |
EGGS | CGS | 99EGGS |
BANANA | BHG | BANANA18 |
table 2:
FNAME | FID |
---|---|
BREAD | 18 |
FISH | 45 |
BACON | 77 |
EGGS | 99 |
currently a simple search is done on table 1 to find id's of food as follows:
SELECT ID1
FROM TABLE1
WHERE NAME IN NAME_LIST
so for example when the name list is: ('BREAD','FISH') then it returns XYZ, ABC. The problem is this misses 'BREAD_OLD' which is an older version with the same ID. (18)
I need to change this so now the search is done based on the FID of food rather than the NAME to find affected foods but I cannot change the input.
eg: given a list of foods: ('BREAD', 'FISH')
the result should be XYZ, ABC, BNQ (because BREAD matches 18 which matches BNQ in table 1)
how can this can be done? I think I need to use a join or do a 'select within a select' but I'm not sure how this would work with multiple inputs.
edit: ORACLE is the database
edit 2: adding BANANA18 to table 1, needs to be leading match
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正确的解决方案是在FID字段上链接。但是,Table1上的FID字段似乎将ID与名称相连。因此,解决方案将是从该字段提取数字值,然后将其链接到表2上的FID字段。例如:
The correct solution would be to link on the FID fields. However, the FID field on table1 seems to concatenate the ID with the name. The solution would therfore be to extract the numeric value from that field and then use that to link it to the FID field on table 2. For example:
假设
table1.fid
等于table2.fid
与table1.fname串联
,那么您不需要(慢)正则表达式,并且可以使用简单平等与字符串串联结合:对于示例数据:
输出:
db<>
Assuming that
table1.FID
equalstable2.FID
concatenated withtable1.FNAME
then you do not need (slow) regular expressions and can use a simple equality combined with string concatenation:Which, for the sample data:
Outputs:
db<>fiddle here
示例数据:
查询:
在查询中,我们在FID上加入Table1和Table2(使用REGEX_SUBSTR提取table1 fid的第一个n数字),因此行'bread_old'将与table2中的行'bread'一起连接,当我们添加条件fname时('面包')将选择“面包”和“面包_old”的ID1。
结果:
Sample data:
Query:
In query we join TABLE1 and TABLE2 on FID (using REGEX_SUBSTR to extract first n numbers of TABLE1 FID) so row 'BREAD_OLD' will be joined with row 'BREAD' from TABLE2 and when we add condition FNAME IN ('BREAD') both ID1 of 'BREAD' AND 'BREAD_OLD' will be selected.
Result:
一个选项是通过匹配
Table2
与提取的子字符串的串联列加入表,直至_
_ fid> fid columntable1
例如One option is joining the tables by matching concatenated columns of
table2
with the extracted substrings upto_
character forfid
column oftable1
such asDemo