【发布时间】:2019-03-19 22:43:56
【问题描述】:
我有一个表格,其中有一列的字符串格式如下:{1,4,5}。它们可以是任意长度,我想针对在该字符串中具有其 ID 的任何值加入一个 ID 表。
这是第一张桌子
name id count
apple {1,3,6} 5
orange {5,3,1} 3
potato {8,1,9} 3
这是第二张桌子 -
id2 category
1 foo
2 foobar
3 candy
4 candybar
5 oreo
6 pistachio
我想为第一个表中列出的具有第二个表中的类别的每个 ID 设置一行。我希望它们看起来像这样 -
id2 name id count
1 apple {1,3,6} 5
1 orange {5,3,1} 3
1 potato {8,1,9} 3
3 apple {1,3,6} 5
3 orange {5,3,1} 3
8 potato {8,1,9} 3
9 potato {8,1,9} 3
这是我到目前为止所得到的。我可以有一个显示join if the value is included 的连接过滤器吗?
select id2, name, id, count
from table2 as t2
left join table1 as t1
on t2.id2 %in% t1.id
【问题讨论】:
-
字符串函数因数据库而异。您使用的是什么特定的数据库? PostgreSQL、Oracle、DB2 等
-
我正在使用 PostgreSQL。
-
不管怎样,id 字段是从
array_agg()函数创建的,但我认为我应该将其转换为文本。
标签: sql postgresql