|id |title |jsonb_structure |
|1 |Widescreen Horror Movies |{"genre":["horror"], "aspect_ratio":["2.39"]} |
|2 |Widescreen digital Films |{"aspect_ratio":["2.39"], "format":["digital"]} |
收集1将返回锯,nosferatu和nope。集合2将返回nope。
添加具有数字格式的2.39宽高比的新电影将被分组到宽屏数字收藏室中
|6 |The Stand |horror |2.39 |Digital |
在现实中,电影表有9列,将来可能还有更多的内容,我想以这种方式过滤。是否可以用一个语句检索标题?这是最好的方法吗?
具有测试数据
Id
titleaspect_ratio
saw | -Horror | 2.39 | ||
---|---|---|---|---|
2 | 米 | digital | ||
digital | 6 | |||
comedy | digital | 7 | Comedy-2宽屏 | |
JSONB_STRUCTURE | 1 |
Allaspect DigitalFilms | ||
---|---|---|
4 | ||
widecreen喜剧 | ||
{“ genre”:[“恐怖”],“ fack_ratio”:[1.85“,” 2.39“],“格式”:[“ 35mm”]} | ||
with allMatch as(
select c.id cid,c.title cTitle, jsonb_structure attrList
,attr, value attrValue -- ,key attrKey
,m.id mid, m.title mTitle, m.genre mGenre, m.aspect_ratio mAspect_ratio,m.format mFormat
,attrCnt
from collection c
cross join lateral( select key attr,value
,count(*)over() attrCnt
from json_each(jsonb_structure )t
) at
left join movies m
on ( at.attr='genre' and
m.genre in (select els.value from json_array_elements_text(value) els)
)
or( at.attr='aspect_ratio' and
m.aspect_ratio in (select els.value from json_array_elements_text(value) els)
)
or( at.attr='format' and
m.format in (select els.value from json_array_elements_text(value) els)
)
)
select cid,min(cTitle)cTitle,mid,min(mTitle) mTitle
,string_agg(attr,',') attrKeys
,count(*) attrCount
,count(distinct attr) dCnt2
,max(attrCnt)attrCnt
from allMatch
group by cid,mid
having count(*)=max(attrCnt)
|
||
ctitle | mid | Mtitle |
attrcount | dcnt2 | attrcnt |
1
wishcreen恐怖电影1wishcreen恐怖电影 | 3 | 2 | |||||
---|---|---|---|---|---|---|---|
1 | 2 | 2 | 2 | ||||
2 | 屏幕数字电影 | 5 | nope | aspect_ratio,格式 | |||
2 | 2 | 2 | |||||
aspect_ratio,格式 | Allaspect DigitalFilms | ||||||
nope | aspect_ratio,格式 | Allaspect DigitalFilms | 6 | ||||
aspect_ratio,格式 | Allaspect喜剧 | 6 | comedy-1 | ||||
widecreen喜剧 | 2 | ||||||
2 | 5 | 7 | Comedy-2宽屏 | 2 | 2 | 2 | |
6 | 35mm恐怖 | saw | aspect_ratio,格式,流派 | ||||
3 | 3 | 3 | 3 | 6 | 35mm恐怖 | ||
3 | 3 | 3 |