我创建了两个表,一个是客户,另一个是ord
select * from customers;
id Name age adress salary
2 102 jpj 24 zzzz 10000
3 103 ftd 20 kkkk 20000
4 104 jin 40 llll 30000
5 105 michael 30 dddd 25000
6 106 das 25 hhhh 10000
7 107 vijay 26 mmmm 12000
8 108 thanku 31 jjjj 26000
9 109 vishnu 34 gggg 24000
10 110 vas 28 ffff 18000
select * from ord;
oid order_date id amount
201 12/11/2013 1:00:00 AM 102 2500
202 12/11/2013 4:14:17 AM 102 3000
203 12/9/2013 9:18:16 PM 103 2000
204 12/8/2013 12:00:00 PM 102 1000
使用查询
select c.name,c.salary,o.amount
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id;
现在它的打印像
1 jpg 10000 1000
2 jpg 10000 3000
3 jpg 10000 2500
4 ftd 20000 2000
5 vijay 12000 2000
但我想打印我的表
name id amount
jpj 102 6500 -> this is the sum of amount in the order table order by 102
ftd 103 2000
vijay 107 2000
请帮我解决这个问题我是sql的首发
答案 0 :(得分:1)
您需要group
数据;例如:
select c.name,c.id,sum(o.amount) as amount
from CUSTOMERS c
inner join ord o on c.id=o.customer_id
group by c.id, c.name