数据库的架构:
user
可以参与多个线程,而thread
可能有多个用户(多对多关系)。
输入:user_id's
(1,2,3,4 ...)的数组。
输出获取所有给定用户参与的所有线程(thread_id's
)(在每个线程中必须有所有给定用户)。
mysql> describe participants;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| thread_id | int(10) unsigned | NO | | NULL | |
| user_id | int(10) unsigned | NO | | NULL | |
| last_read | timestamp | YES | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
这是表的架构:
任何人都可以通过mysql
查询来帮助我获得结果吗?
答案 0 :(得分:1)
您可以通过聚合
根据您的标准获取线程select p.thread_id
from participants p
where p.user_id in(1,2,3,4)
group by p.thread_id
having count(distinct p.user_id) = @count_of_ your_ids /* like 4 */