现在存在一下两个表
select * from a;
ID PARENT_ID
-- ---------
11 1
12 1
13 1
14 2
15 2
16 2
17 3
18 3
19 3select * from b;
ID TOTAL
-- ----------
11 10
12 12
13 16
14 16
15 13
16 11
17 13
18 10
19 15
我现在想得到,按a表中的PARENT_ID字段进行分类,求的b表中的total字段的和
我怎么用一条SQL语句得到下面三条SQL返回的结果呢?
select '1' parent_id, sum(total) from b where id in(select id from a where parent_id='1');PARENT_ID SUM(TOTAL)
--------- ----------
1 38select '2' parent_id, sum(total) from b where id in(select id from a where parent_id='2');PARENT_ID SUM(TOTAL)
--------- ----------
2 40select '3' parent_id, sum(total) from b where id in(select id from a where parent_id='3');PARENT_ID SUM(TOTAL)
--------- ----------
3 38
并且要求将返回结果按a表中parent_id字段进行排序
有高手请指点一下
select * from a;
ID PARENT_ID
-- ---------
11 1
12 1
13 1
14 2
15 2
16 2
17 3
18 3
19 3select * from b;
ID TOTAL
-- ----------
11 10
12 12
13 16
14 16
15 13
16 11
17 13
18 10
19 15
我现在想得到,按a表中的PARENT_ID字段进行分类,求的b表中的total字段的和
我怎么用一条SQL语句得到下面三条SQL返回的结果呢?
select '1' parent_id, sum(total) from b where id in(select id from a where parent_id='1');PARENT_ID SUM(TOTAL)
--------- ----------
1 38select '2' parent_id, sum(total) from b where id in(select id from a where parent_id='2');PARENT_ID SUM(TOTAL)
--------- ----------
2 40select '3' parent_id, sum(total) from b where id in(select id from a where parent_id='3');PARENT_ID SUM(TOTAL)
--------- ----------
3 38
并且要求将返回结果按a表中parent_id字段进行排序
有高手请指点一下
(未测试!)
order by a.PARENT_ID