PHP SQL选择ID数组中的位置并具有相同的可变列值 [英] PHP SQL select where in array of IDs and has same variable column value
本文介绍了PHP SQL选择ID数组中的位置并具有相同的可变列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我的桌子有点像下面的桌子.
So I have a table kind of like the one below.
+----+----------+---------+
| ID | group_id | user_id |
+----+----------+---------+
| 1 | 10 | 9 |
| 2 | 20 | 8 |
| 3 | 10 | 7 |
| 4 | 10 | 6 |
| 5 | 20 | 5 |
+----+----------+---------+
我有一个用户ID数组[9,7,6]
,我想获取所有ID都在同一个组中的Group ID吗?
I have an array of user IDs [9,7,6]
and I'd like to get the Group ID where all the IDs are in the same group?
我当前的查询
SELECT group_id FROM group_user WHERE user_id IN (9,7,6) GROUP BY group_id
请记住,我对SQL查询不是很熟悉.
Bare in mind I'm not overly familiar with SQL queries.
谢谢
编辑
我打开了一个新的问题,提供了更多细节和目标.
I have opened a new question with more detail and my goal.
推荐答案
您可以使用条件聚合来查找具有所需user_id与之关联的group_id.
You can use conditional aggregation to find group_id's which have the required user_id's associated with them.
SELECT group_id
FROM group_user
group by group_id
having sum(case when user_id IN (9,7,6) then 1 else 0 end) = 3
这篇关于PHP SQL选择ID数组中的位置并具有相同的可变列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文