如何应用对单个值求和列的单个查询

时间:2015-11-19 01:38:28

标签: sql

我有2个名为userstatistics

的表

user表有3列:idnamecategory

statistics表有3列:ididUser(关系),cal

类似的东西:

用户

 Id name  category
 1  name1 1
 2  name2 2
 3  name3 3

统计

Id idUser cal
1  1      1
2  1      1
3  1      1
4  2      1
5  2      1

如何应用每个用户类别对cal列求和的查询,并给我这样的内容:

category totalcal
1        3
2        2 
3        0

2 个答案:

答案 0 :(得分:0)

使用String.IsNullOrEmpty(name)获取LEFT JOIN的总数0

category=3

此处SELECT user.category ,SUM(statistics.cal) AS totalcal FROM user LEFT JOIN statistics ON statistics.idUser = user.Id GROUP BY user.category 会为SUM返回NULL。要获得category=3而不是0,您可以使用NULL

答案 1 :(得分:0)

您想要angular.module("myApp").directive("myDirective", function(){ return { link: function(scope, elem, attrs) { grecaptcha.render(attrs.id, {'sitekey' : 'captcha-key-here'}); }; }; }); 来保留所有类别。其余的只是聚合:

git commit --amend