Mysql 有没有办法把同一列和另一列求和?

Mysql 有没有办法把同一列和另一列求和?,mysql,Mysql,我有一个名为“menergy”的表,其中包含下一个结构和数据示例: -------------------------------------------------------------- time | m_value | pt_periode | meter_id_meter | mtype_id_type | -------------------------------------------------------------- t1 | 10 | 0

我有一个名为“menergy”的表,其中包含下一个结构和数据示例:

--------------------------------------------------------------
time | m_value | pt_periode | meter_id_meter | mtype_id_type |
--------------------------------------------------------------
t1   | 10      |  0         |      1         |      24       |
t1   |  5      |  0         |      1         |      25       |
t1   | 15      |  0         |      1         |      26       |
t1   | 20      |  0         |      1         |      27       |
t1   | 500     |  0         |      1         |      0002     |
有4种类型的
mtype
因此,在mtype=24、25、26、27和非0002的情况下,条件是:
相同的时间、相同的pt\u周期、相同的仪表id
求和它们的
m\u值
。 例如,条件可能是:

  (meter_id_meter = 1 and mtype_id_type = 24 and pt_periode = 0) or
  (meter_id_meter = 1 and mtype_id_type = 25 and pt_periode = 0) or
  (meter_id_meter = 1 and mtype_id_type = 26 and pt_periode = 0) or
  (meter_id_meter = 1 and mtype_id_type = 27 and pt_periode = 0)
最后一个数据示例的结果是:10+5+15+20=
50
。所以我想通过手术得到50分 我已经读过IF条件和JOIN,但它需要创建另一个表。但是,我尝试了下一步,但出现了语法错误:

选择
时间戳将(月-1,时间戳)添加为“时间”,
如果((仪表id\U仪表=1,mtype\U id\U type=24,pt\U周期=0)或
(仪表id\U仪表=1,mtype\U id\U type=25,pt\U周期=0)或
(仪表id\U仪表=1,mtype\U id\U type=26,pt\U周期=0)或
(仪表id仪表=1,mtype仪表id类型=27,pt周期=0),总和(m值)为“Q”
来自menergy

查看代码时,似乎需要求if条件为true时的值之和,因此请尝试使用where

SELECT  timestampadd(month, -1, time_stamp) as "time", SUM(m_value)) as "Q"

FROM menergy
WHERE  (
  (meter_id_meter = 1 and mtype_id_type = 24 and pt_periode = 0) or
  (meter_id_meter = 1 and mtype_id_type = 25 and pt_periode = 0) or
  (meter_id_meter = 1 and mtype_id_type = 26 and pt_periode = 0) or
  (meter_id_meter = 1 and mtype_id_type = 27 and pt_periode = 0) 
)
group by  timestampadd(month, -1, time_stamp)
或者干脆

WHERE meter_id  = 1  AND pt_periode = 0 and mtype in (24,25,26,27)

我怀疑你应该使用where子句

SELECT   SUM(m_value)) as "Q"
FROM menergy
where meter_id_meter = 1 and pr_periode = 0 and mtype_id_type in (2,25,26,27)

但是我不知道你想用timestadd(月-1,时间戳)作为“时间”做什么,

你还没有真正说明你想做什么。样本数据和预期输出将有助于澄清(作为文本)。