本文介绍了如何执行此汇总?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个表,一个是客户,另一个是ord

I have crated two table one is customers and the other one is 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的入门者

please help me to solve this problem I am a starter in sql

推荐答案

您需要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

这篇关于如何执行此汇总?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 16:51