subtract two numbers myql code example

Example: how to subtract of two numbers in mysql

select sum(s0.w - s1.w) as total_weight_loss
from (select idMember, max(weight) as w from Measurements m
      where m.date = (select min(date)  from Measurements n
                      where m.idMember = n.idMember)
      group by idMember) s0
join (select idMember, min(weight) as w from Measurements m
      where m.date = (select max(date)  from Measurements n
                      where m.idMember = n.idMember)
      group by idMember) s1
on s0.idMember = s1.idMember

Tags:

Sql Example