我的Postgresql DB中有一个基本的多对多关系。
表-用户
身份证名..。
桌聊
id名
桌面聊天器
id userId chatId
我想得到所有的聊天(实际上,只有一个这样的聊天),这将只有用户,由他们的ids定义。
例如,我有id 1的用户、id 2的用户和id 3的用户,如何才能得到只包含这些用户的聊天,而不是更多的聊天。这意味着,只与这些用户中的某个人聊天将不包括在结果中。只有包含这些用户的聊天
我明白,肯定已经有这样的问题了,但我没能找到。
发布于 2017-04-02 15:04:41
下面是一种使用group by和having的方法
select cu.chatid
from chatuser cu
group by cu.chatid
having sum(case when cu.userid not in (1, 2, 3) then 1 else 0 end) = 0;这将计算不在列表中的用户数量。= 0说chatId没有任何一个。
在Postgres中,可以将其缩短为:
select cu.chatid
from chatuser cu
group by cu.chatid
having sum( (cu.userid not in (1, 2, 3))::int) = 0;https://stackoverflow.com/questions/43169837
复制相似问题