php - 选择仅具有一个值的优先级数组

标签 php sql postgresql

我有视频表:

如何获取对请求类别定义较好的记录并忽略相似。

SELECT [...] WHERE category IN(1,2,3,4);

预期结果:

最佳答案

关键是找到具有最小基数的匹配类别:

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)

关于php - 选择仅具有一个值的优先级数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35123919/

相关文章:

php - 与 mysql 和路径交叉检查

php - 在带循环的准备好的语句后输出数据时出现问题

mysql - SQL 填充常量以防连接两个表时不匹配

mysql - 表格优化

ruby - 为什么我可以从 JSONB 值更新 PostgreSQL 列字符串而不是 Sequel 中的整数?

javascript - 在JS中读取CakePHP Cookie值

php - mysql_query 不起作用

MySQL 错误代码 : 1349. View 的 SELECT 包含 FROM 子句中的子查询

sql - PostgreSQL : Achieve check constraint on aggregated SUM

postgresql - Postgres 动态 COPY 语句