How to store sum(field_name) in a variable within MySql Select statement?

喜你入骨 提交于 2020-12-06 13:15:25

问题


I am trying to store the sum total of a particular field in a variable like @data := sum(field_name) within MySQL Select statement.

Below is an exact example of my query:

SELECT a.id, @data1:=sum(b.amount) amount, @data1 as returned_val
FROM tbl_table1 a 
LEFT JOIN tbl_table2 b ON b.acount_id=a.id
GROUP BY a.id

Notice that I store the sum(b.amount) to a variable @data1 and tried to display it in another row but never work as what I'm expecting.

Is there any other way doing this?

来源:https://stackoverflow.com/questions/12578298/how-to-store-sumfield-name-in-a-variable-within-mysql-select-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!