MySQL 分页查某团队中成员参与的所有 app

表:
user(id)
app(id)
member(user_id, master_id)
app_member(app_id, user_id, master_id)

其中 user_id, master_id 对应的是 user 表的 id
member 是成员表
团队中的 master 可以创建 app,然后添加 member 参与他创建的 app

分页查某团队中成员参与的所有 app

select a.*, GROUP_CONCAT(b.app_name) as app_list 
from member a left join
(select app_member.user_id as user_id, app_member.master_id as master_id, app_name
from app_member join app on app_member.app_id = app.id) b 
on a.user_id = b.user_id and a.master_id = b.master_id where a.master_id = 4688706 group by id order by created_at limit 0, 10

但是这种子查询会联合查询出整张表,所以分成两部分查询比较好:
1、先分页查出成员

select * from member where master_id = 4688706 order by created_at limit 0, 10

2、用 in 查询查出这些成员参与的 app

# 括号中是第一步查出来的 member 的 user_id
select * from app_member where user_id in (...)

3、在业务层联合

你可能感兴趣的:(MySQL 分页查某团队中成员参与的所有 app)