mysql匹配不上为0,Mysql计数如果没有匹配则返回零

I have a set of approx 9000 tutor ids in an array and i have put them in a string like:

(1,2, 3, 4,5,6,7,....9000,9001,9002)

so that i can use them in the following query:

select count(student_assignment.assignment_id) as total_assignment from

student_assignment, assigned_tutor_fk where assignment_status = 'closed'

and assigned_tutor_fk in (1,2, 3, 4,5,6,7,..100,101,103...9000,9001,9002)

group by assigned tutor_fk.

I want to calculate total number of rows associated with each tutor(assigned_tutor_fk), and those tutors which do not have an assignment ie those which do not have assignment

record in the table i want to show their assignment count as 0, and i just want my query to return count and assigned_tutor_fk

my table structure is:

assignment_id | assigned_tutor_fk | assignment_date | student_id |

| 1 | 2 | 22-01-2011 | 4 |

| 2 | 3 | 14-03-2011 | 5 |

Im trying to get my output to be like this:

|total_assignment | assigned_tutor_fk |

| 5 | 4 |

| 2 | 7 |

| 0 | 8 |

Update: I tthink i have not been able to express myself properly,i already have a list of tutors filtered on another criteria, it was very complex to combine these two queries so now i have a set of the tutor id's and i want the sum to be displayed as zero in case the tutors does not have assignment record. please help me on this as i don know wht to do now

解决方案SELECT t.id, COUNT(sa.assignment_id)

FROM tutor t

LEFT JOIN

student_assignement sa

ON sa.assignment_tutor_fk = t.id

WHERE t.id IN (1, 2, ..., 9002)

GROUP BY

t.id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值