显示有多少用户已接受某些项目的请求(即待处理= 0),以发送请求的用户名作为前缀。
items :
item_id item_name
1 I1
2 I2
profile :
user_id name
1 A
2 B
3 C
4 D
user_items :
user_id shared_by_user_id pending item_id
1 2 1 1
3 2 0 1
4 2 0 1
1 2 1 2
预期输出
item_name users shared_by_user_id
I1 B,C,D 2
I2 B 2
我编写了以下查询,其输出如下 -
select item_name, concat(t4.name, ',',group_concat(t3.name)) as users, t1.shared_by_user_id
from user_items t1
left join item t2 on t2.item_id = t1.item_id
left join profile t3 on t3.user_id = t1.user_id
left join profile t4 on t4.user_id = t1.shared_by_user_id
where t1.shared_by_user_id = 2
group by t2.item_id;
实际输出
item_name users shared_by_user_id
I1 B,A,C,D 2
I2 B,A 2
显然,剩下的问题是在 GROUP_CONCAT 期间添加一个用于过滤挂起 = 1 记录的条件,但无法找到如何操作..
最佳答案
这是您想要的 group_concat()
表达式吗?
concat(t4.name, ',', group_concat(case when pending = 1 then t3.name end)) as users
group_concat()
忽略 NULL
值。
关于mysql - GROUP_CONCAT WHERE 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22048854/