gpt4 book ai didi

postgresql - 选择 ids 数组的公共(public)元素 Postgresql

转载 作者:行者123 更新时间:2023-11-29 13:48:44 24 4
gpt4 key购买 nike

所以我有这个任务要在 Postgresql 中解决,对于用户提供的给定数量的 classId,我必须向他返回所述类的公共(public)属性。

我有三个表来表示模型(一个类有多个属性,一个属性可以在多个类中)

表类:

---------------------------
| Id | Name | Description |
---------------------------

表格属性:

---------------------------
| Id | Name | Description |
---------------------------

最后是 ClassProperties 表

------------------------
| ClassId | PropertyId |
------------------------

所以用户给了我一个包含 classId 的数组,我必须返回他所有类的所有公共(public)属性(就像我上面说的)

截至目前,我只能使用以下代码返回所有类的每个属性:

select p.*
from properties as p
inner join ClassProperties as cp on cp.propertyid= p.id
where cp.classid = any ('{88d5fe8f-e19e-40b4-bc65-83ac64f825b0, a2a63bea-
aeee-4d3b-817e-cc635383c571}') ;

您看到的 id 是 Guid,但这并不重要。任何帮助,将不胜感激。谢谢!

最佳答案

您可以这样做:

select p.*
from properties p inner join
ClassProperties cp
on cp.propertyid = p.id
where cp.classid = any ('{88d5fe8f-e19e-40b4-bc65-83ac64f825b0, a2a63bea-aeee-4d3b-817e-cc635383c571}')
group by p.id
having count(*) = 2;

如果允许重复,count(*) 应该是 count(distinct classid)

注意:值“2”是您要检查的元素数。您还可以使用 array_length('{88d5fe8f-e19e-40b4-bc65-83ac64f825b0, a2a63bea-aeee-4d3b-817e-cc635383c571}')

关于postgresql - 选择 ids 数组的公共(public)元素 Postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44327778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com