Mysql GROUP_CONCAT WHERE子句
显示有多少用户已接受某个项目的请求(即待定=0),前缀为发送请求的用户的名称Mysql GROUP_CONCAT WHERE子句,mysql,Mysql,显示有多少用户已接受某个项目的请求(即待定=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
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
预期产量
我编写了以下查询,其输出如下-
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;
实际产量
显然,剩下的问题是在GROUP_CONCAT()过程中添加一个过滤挂起=1记录的条件,但无法找到如何进行过滤。这是您想要的
GROUP_CONCAT()表达式吗
group\u concat()
忽略NULL
值。我还在group\u concat之前添加了IFNULL,否则concat会将其设置为NULL
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
concat(t4.name, ',', group_concat(case when pending = 1 then t3.name end)) as users