如何在同一个表上使用union查询和mysql中的sum(value)?

时间:2016-04-09 07:17:06

标签: mysql union

我有一张桌子:

OC  Period(MM/YYYY) Ing
CHO 04/01/2016      100
CHO 04/01/2016      200
CHO 04/01/2016      0
CFE 04/01/2016      400
CFE 04/01/2016      350
CFE 04/01/2016      0

我有一个像这样的SQL查询:

  (SELECT OC,Ing FROM contacts c where ing > 0)
  UNION ALL
  (SELECT OC,Sum(Ing) FROM contacts c where ing = 0 Group by OC)

我想得到这样的结果:

Ref Period(MM/YYYY) Ing
CHO 04/01/2016      100
CHO 04/01/2016      200
CHO 04/01/2016      300
CFE 04/01/2016      400
CFE 04/01/2016      350
CFE 04/01/2016      750

1 个答案:

答案 0 :(得分:0)

int var = 2;
cout << "var' address and value: " << &var << " " << var << endl;
cout << (int*)(&var) << endl;
cout << *(int*)(&var) << endl;
cout << (int*)*(int*)(&var) << endl;


int *ptr = &var;
cout << "ptr's address: " << &ptr << endl;
cout << (int*)(&ptr) << endl;
cout << *(int*)(&ptr) << endl;//NOT var's address, WHY?
cout << (int*)*(int*)(&ptr) << endl; //var's address,
cout << *(int*)*(int*)(&ptr) << endl; //var's value,

如果需要对输出进行排序

,可以将其包装在另一个(SELECT c.OC, c.Period, c.Ing FROM contacts c where c.ing > 0) UNION ALL (SELECT c.OC, c.Period, Sum(c.Ing) AS Ing FROM contacts c Group by c.OC, c.Period) 语句中