Postgres使用和条件将所有数组值匹配到同一列

编程入门 行业动态 更新时间:2024-10-22 19:31:18
本文介绍了Postgres使用和条件将所有数组值匹配到同一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有带有以下列的表table_a

I have table table_a with following columns

id event_id 1 101 1 102 1 103 2 105 2 103 2 106

我想使用与条件类似的IN查询来搜索(101,103)和条件类似的条件

I want to search (101, 103) with and conditions similar to IN query with OR condition

让id表示另一个表event_categories的ID是异类的。 / p>

let say id is foreign of another table event_categories having relation like this.

id parent_id 101 null 102 101 103 101 104 null 105 104

所以我想基于AND与父事件类别一起从table_a中获取记录,或在该父级的子事件类别内。

so I want to fetch records from table_a based on AND with parent event category, OR within sub-event categories of that parent.

附加的屏幕截图以进行更多说明

Attached screenshot for more clarification

例如美术馆和展览,节日,美食 &

For example "Art Galleries & Exhibits", "Festivals", "Food & Wine" with AND condition,

美术馆和所有子类别中的所有子类别。展品中的或条件工艺工作室或艺术与艺术学院

All sub-category of "Art Galleries & Exhibits" with OR condition "Art & Craft Workshops" OR "Art & Craft Fairs" OR "Visual Arts" if checked.

表事件

id ------- 24445 24446 24447 24448 24449

event_categories 表

id | name | parent_id -----+--------------------------+----------- 55 | Art & Craft Workshops | 25 53 | Art & Craft Fairs | 25 25 | Art Galleries & Exhibits | 14 | Carnivals | 3 56 | Celebrations | 3 64 | Chili Cook-offs | 26 47 | Circus | 13 45 | Comedy | 13

关联表事件和 event_categories event_id是事件表的外键

event_id | event_category_id ----------+------------------- 24590 | 1 24590 | 13 24445 | 1 24445 | 13 24591 | 1

推荐答案

第一个问题:

SELECT id FROM table_a WHERE id IN (101, 103) GROUP BY id HAVING COUNT(DISTINCT id) = 2

更多推荐

Postgres使用和条件将所有数组值匹配到同一列

本文发布于:2023-10-23 05:56:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   条件   Postgres

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!