繁体   English   中英

SQL 加入一个表,该表有一个数组字段与另一个表的主键

[英]SQL join a table that has an array field with the primary key of another table

我有一个名为 Patients 的表,它有一个名为 related_forms_ids 的字段,它有一个 forms id 数组,并引用了 forms 表上的表单 id

[
 'formId1',
 'formId2',
 'formId3'
]

我有包含这些字段的 forms 表

{
 formId,
 formName
}

我需要使用 related_forms_ids 和 formName 字段将患者表与 forms 表连接起来,以便获得患者 forms 的表单名称。我该怎么做? 我正在尝试使用 Google BigQuery 进行查询

我想你想要的是这个

 SELECT P.*, F.Name
 FROM (SELECT *, UNNEST(related_forms_ids) AS related_form_id
       FROM Patient
 ) AS P
 LEFT JOIN Forms F ON P.related_form_id = F.id

这将为您提供多行中的所有 forms

暂无
暂无

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

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