问题
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