我有视频表:
如何更好地定义所请求类别的记录并忽略类似记录。
SELECT [...] WHERE category IN(1,2,3,4);
预期结果:
答案 0 :(得分:1)
关键是找到具有最小基数的匹配category
:
with given_data(cat) as (
select * from unnest (array[1,2,3,4])
)
select distinct on(cat) *
from urls
cross join given_data
where cat = any(category)
order by cat, cardinality(category);
id | url | category | cat
----+----------+-----------+-----
1 | example1 | {1,2,3,4} | 1
2 | example2 | {2} | 2
3 | example3 | {3} | 3
4 | example4 | {4} | 4
(4 rows)