SELECT
(@a:=SUM(sc_gpa*sub_credit)) AS All_result, #ผลรวมเกรด
(@b:=SUM(sub_credit)) AS CreditTotal # ผลรวมหน่วยกิต
(@a / @b) AS Div_Result # ผลหาร
FROM school
select All_result, CreditTotal, (All_result / CreditTotal) as Div_Result from (
SELECT
SUM(sc_gpa*sub_credit) AS All_result,
SUM(sub_credit) AS CreditTotal, @a as testA, @b as testB
FROM(
SELECT 20 as sc_gpa, 25 as sub_credit union ALL
SELECT 25, 30 union ALL
SELECT 25, 30 union ALL
SELECT 25, 30
)as school
) as temp
SELECT (@a:=SUM(sc_gpa*sub_credit)) AS CreditTotal, (@b:=SUM(sub_credit)) AS AllTotal,(@a:=SUM(sc_gpa*sub_credit)) / (@b:=SUM(sub_credit)) AS Totel FROM school