TABLE_A มีข้อมูลดังนี้
--------------------------------------
id | name
--------------------------------------
1 | X
2 | Y
3 | Z
--------------------------------------
=======================================
TABLE_B
ประกอบด้วย Field
id
table_a_id
amount
=======================================
ต้องการ query ข้อมูลเพื่อให้ได้การแสดงผลดังนี้ครับ
QUERY_TABLE
-------------------------------------------------------------
name | amount
-------------------------------------------------------------
X | 30
Z | 20
Y | 5
--------------------------------------------------------------
SELECT `TABLE_A`.`name` as 'name' , SUM( `TABLE_B`.`amount`) as 'amount'
FROM `TABLE_A` , `TABLE_B`
WHERE `TABLE_A`.`id` = `TABLE_B`.`tabel_a_id`
GROUP BY `TABLE_A`.`id` ORDER BY amount