mysql 查询数据为空如何设置默认值

clipboard.png
如何把查询出来的null 默认为0;

SQL是:select name,sum(score) as cou from student left join score on (student.id=score.stu_id) group by stu_id order by cou desc;

求指导

阅读 15.3k
2 个回答
select name,sum(if(score is null,0,score)) as cou from student left join score on (student.id=score.stu_id) group by stu_id order by cou desc;

试一下这个

综合一楼和一楼评论,应该用在sum累加score的值前,使用ifnull(expr1,0)判断score列值是否为空,为空则以0计算

select name,sum(ifnull(score,0)) as cou from student left join score on (student.id=score.stu_id) group by stu_id order by cou desc;
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏