数据表: data
id name amount
1 aa 4
2 bb 2
3 cc 1
4 aa 10
5 aa 2类别表: sort
id name
1 aa
2 bb
3 cc
4 dd
5 ee想得到的数据为
name amount
aa 16
bb 2
cc 1
dd 0
ee 0用什么SQL语句可将类别表sort在data中有出现过的数据累加,而没在data中有数据的则为0显示出来,显示NULL值的则用0显示自己做了一句SQL语句,但感觉好象有点怪怪的.select name, ( select if(sum(amount)>0, sum(amount), 0) from data where name=sort.id ) as amount from sort上面虽然能得到我想要的数据,但好象又感觉怪怪的,有没有更理想点的SQL语句?
id name amount
1 aa 4
2 bb 2
3 cc 1
4 aa 10
5 aa 2类别表: sort
id name
1 aa
2 bb
3 cc
4 dd
5 ee想得到的数据为
name amount
aa 16
bb 2
cc 1
dd 0
ee 0用什么SQL语句可将类别表sort在data中有出现过的数据累加,而没在data中有数据的则为0显示出来,显示NULL值的则用0显示自己做了一句SQL语句,但感觉好象有点怪怪的.select name, ( select if(sum(amount)>0, sum(amount), 0) from data where name=sort.id ) as amount from sort上面虽然能得到我想要的数据,但好象又感觉怪怪的,有没有更理想点的SQL语句?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货