简体   繁体   English

将一个表中的外键数组与另一表中的主键匹配?

[英]Match an array of foreign keys in one table with primary key in another table?

I have a two tables.我有两张桌子。 One like this:一个是这样的:

id   | array_of_ids
----------------------
 001 | {012, 345, 789}
 002 | {789, 123, 456}
 003 | {234, 789, 567}
 004 | {543, 210, 789}

Another like this:另一个像这样:

 ids  | str
-------------
 012 | am_name1
 345 | name2
 789 | name3
 123 | am_name4
 456 | name5
 234 | name6
 567 | am_name7
 543 | am_name8
 210 | name9

I want to create a table that looks like this:我想创建一个看起来像这样的表:

id   | array_of_ids    | label
-----------------------------
 001 | {012, 345, 789} | name1
 002 | {789, 123, 456} | name4
 003 | {234, 789, 567} | name7
 004 | {543, 210, 789} | name8

I know that which label gets populated looks random, but here's some more details: every id has a corresponding name, but i am only interested in certain type of name -- the one's with the prefix 'am'.我知道哪个标签被填充看起来是随机的,但这里有一些更多的细节:每个 id 都有一个相应的名称,但我只对某些类型的名称感兴趣——带有前缀“am”的名称。 I want to be able to scan the array_of_ids , check if a id in the array matches a str i am interested in and the populate a new variable label with the correspond name.我希望能够扫描array_of_ids ,检查数组中的 id 是否与我感兴趣的str匹配,并使用相应的名称填充新变量label I hope this is clear!我希望这很清楚! Happy to edit if necessary!如有必要,很乐意编辑!

unnest() and join : unnest()join

select t1.id, t1.array_of_ids,
       max(case when regexp_like(t2.name, '^am_') then substr(t2.name, 4) end)
from table1 t1 cross join
     unnest(t1.array_of_ids) t1_id(id) join
     table2 t2
     on t2.id = t1_id.id
group by t1.id, t1.array_of_ids;

I realized that I wasn't making use of the PRESTO's array functions .我意识到我没有使用 PRESTO 的数组函数 This solution is a little clunky since it requires you to look up each id/name pair outside of the solution, but it works.这个解决方案有点笨拙,因为它需要您在解决方案之外查找每个 id/name 对,但它有效。

SELECT id, array_of_ids,
  CASE WHEN CONTAINS(array_of_ids, 012) = TRUE THEN 'name1'
       WHEN CONTAINS(array_of_ids, 123) = TRUE THEN 'name4'
       WHEN CONTAINS(array_of_ids, 567) = TRUE THEN 'name7'
       WHEN CONTAINS(array_of_ids, 543) = TRUE THEN 'name8'
       ELSE NULL END AS label
FROM table1

暂无
暂无

声明:本站的技术帖子网页,遵循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? SQL中的表能否将多列作为仅引用另一表的一个主键的外键? - Can a table in SQL have multiple columns as foreign keys that refer only to one primary key of another table? SQL 两个外键链接到另一个表的一个主键以从该表中提取字段 - SQL Two foreign keys linked to one primary key of another table to pull field from that 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? 引用表中没有与外键中的列列表匹配的主键或候选键 - No Primary or candidate keys in the referenced table that match column list in foreign key 更新一个表中的主键,这是另一表中的外键 - Update primary key in one table which is foreign key in another table 两个外键引用另一个表的主键 - Two foreign keys reference the primary key of another table 从表中使用多个外键选择一个主键 - Select one Primary Key using multiple Foreign Keys from a table 在 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM