0

我有两个表 -
表 1:Types

ID | Type
---+------
1  | type1
2  | type2
3  | type3
4  | type4
5  | type5
6  | type6

表 2:Details

ID | Type   | EventName | Cost
---+--------+-----------+------
1  | type1  | name1     | 500
2  | type1  | name2     | 500
3  | type2  | name3     | 500
4  | type3  | name4     | 1500
5  | type3  | name5     | 1000
6  | type3  | name6     | 1000

两个表的预期结果:

Type   | Number | Cost
-------+--------+--------------
type1  | 2      | 1000
type2  | 1      | 500
type3  | 3      | 3500
type4  | 0      | 0
type5  | 0      | 0
type6  | 0      | 0

我试过了LEFT JOIN。但它没有给我正确的数字。

4

3 回答 3

1
SELECT t.Type, count(d.Type), sum(d.cost) 
FROM Types t LEFT JOIN Details d ON t.Type=d.Type
group by t.Type,d.Type
于 2013-11-04T06:57:20.817 回答
0
SELECT 
  t.Type, 
  count(t.type) as Number, 
  sum(d.cost) as Cost 
FROM 
  Types t 
  INNER JOIN Details d 
     ON t.type=d.type 
  GROUP BY t.type
于 2013-11-04T06:55:50.743 回答
-1

尝试 SELECT * FROM (t.type), (t.EventName) WHERE sum(d.cost)

于 2013-11-04T06:56:08.413 回答