mysql 中的 Sum 函数与内部查询
Sum function in mysql with inner query
我有如下查询:
select b.*, SUM(h.points) as points
from brands b left join
histories h
on b.id = h.brandid and h.userId = $userId
where b.id=$brandId and b.active=1
group by b.id
limit 1
我只想对以下情况求和:
where h.type != 10, h.points !=0 and points should be between two dates like this: h.time between '2016-06-01' and '2016-06-31'...
我想这可以通过使用子查询来完成,但我不确定如何...你们能帮帮我吗???
所需的输出将是:
userId brandId points
1 64 155
1 15 100
依此类推,对于在两个日期之间获得这些分数并且分数类型不是 != 10;
的每个用户,我都很好地总结了每个分数
table 结构如下:
Brands => Histories <= Users
select b.id,
sum(case when h.type <> 10 and h.time between '2016-06-01' and '2016-06-31' then h.points
else 0 end) as points
from brands b
left join histories h on b.id = h.brandid and h.userId = $userId
and b.active=1 and b.id=$brandId
group by b.id
我有如下查询:
select b.*, SUM(h.points) as points
from brands b left join
histories h
on b.id = h.brandid and h.userId = $userId
where b.id=$brandId and b.active=1
group by b.id
limit 1
我只想对以下情况求和:
where h.type != 10, h.points !=0 and points should be between two dates like this: h.time between '2016-06-01' and '2016-06-31'...
我想这可以通过使用子查询来完成,但我不确定如何...你们能帮帮我吗???
所需的输出将是:
userId brandId points
1 64 155
1 15 100
依此类推,对于在两个日期之间获得这些分数并且分数类型不是 != 10;
的每个用户,我都很好地总结了每个分数table 结构如下:
Brands => Histories <= Users
select b.id,
sum(case when h.type <> 10 and h.time between '2016-06-01' and '2016-06-31' then h.points
else 0 end) as points
from brands b
left join histories h on b.id = h.brandid and h.userId = $userId
and b.active=1 and b.id=$brandId
group by b.id