mySQL的sql结构最大量是多少?

时间:2011-10-04 03:25:21

标签: mysql sql

TABLE_A有字段(id,name)

--------------------------------------
id   |    name
--------------------------------------
1   |     X

2   |     Y

3   |     Z
--------------------------------------

TABLE_B有字段(id,table_a_id,amount)

=====================================
id  |   table_a_id   |  amount 
=====================================
1   |    1                | 10

2   |    1                |  5

3   |    2                |  5 

4   |    1                |  5

5   |    3                |  20

6   |    1                |  10

=======================================

我希望显示结果。请帮助解决mySQL的查询结构。谢谢。

=====================================
name    |    amount
=====================================
X           |    30

Z           |    20

Y           |    5
--------------------------------------------------------------

1 个答案:

答案 0 :(得分:2)

这就是你需要的:

select max(ta.name) name, coalesce(sum(tb.amount), 0) amount
from Table_A ta
left join Table_B tb
on ta.id = tb.table_a_id
group by ta.id

LEFT JOIN将确保如果表A中的W在表B中没有行,则将显示W。