SUM中的子查询

时间:2015-04-26 23:11:10

标签: mysql sql group-by sum

我正在使用MySQL,我有两张桌子。一个具有对值的引用,另一个具有实际值。我想计算SUM

ItemMess

+----+--------+----------+---------------------+
| ID | ItemID | MessType | TimeStamp           |
+----+--------+----------+---------------------+
| 1  | 2      | 1        | 2015-04-26 18:53:33 |
+----+--------+----------+---------------------+
| 2  | 2      | 2        | 2015-04-26 18:54:42 |
+----+--------+----------+---------------------+
| 3  | 2      | 1        | 2015-04-26 18:54:49 |
+----+--------+----------+---------------------+
| 4  | 2      | 1        | 2015-04-26 18:55:01 |
+----+--------+----------+---------------------+
| 5  | 2      | 2        | 2015-04-26 18:55:04 |
+----+--------+----------+---------------------+

MessTypes

+------------+--------------+---------------+
| MessTypeID | MessTypeName | MessTypeValue |
+------------+--------------+---------------+
| 1          | Mess Up      | 1             |
+------------+--------------+---------------+
| 2          | Mess Down    | -1            |
+------------+--------------+---------------+

因此,如果您看到,MessTypeID引用了MessTypes表中的值。我想要sumup的{​​{1}}。所以,如果我进行如下查询:

down

我得到了这个输出:

SELECT *, (
  SELECT `MessTypeValue` FROM `MessTypes` WHERE `MessTypeID`=`MessType`
) AS `Val` FROM `ItemMess` GROUP BY `ItemID`

但如果我给出:

+----+--------+----------+---------------------+
| ID | ItemID | MessType | TimeStamp           |
+----+--------+----------+---------------------+
| 1  | 2      | 1        | 2015-04-26 18:53:33 |
+----+--------+----------+---------------------+
| 2  | 2      | 2        | 2015-04-26 18:54:42 |
+----+--------+----------+---------------------+
| 3  | 2      | 1        | 2015-04-26 18:54:49 |
+----+--------+----------+---------------------+
| 4  | 2      | 1        | 2015-04-26 18:55:01 |
+----+--------+----------+---------------------+
| 5  | 2      | 2        | 2015-04-26 18:55:04 |
+----+--------+----------+---------------------+

或者

SELECT *, (
  SELECT `MessTypeValue` FROM `MessTypes` WHERE `MessTypeID`=`MessType`
) AS `Val`, SUM(SELECT `MessTypeValue` FROM `Messtypes` WHERE `MessTypeID`=`MessType`) AS `Mess`FROM `ItemMess` GROUP BY `ItemID`

我收到以下错误:

  

查询错误(1064):第1行'SELECT SELECT *, ( SELECT `MessTypeValue` FROM `MessTypes` WHERE `MessTypeID`=`MessType` ) AS `Val`, SUM(`Val`) AS `Mess`FROM `ItemMess` GROUP BY `ItemID` FROM MessTypeValue WHERE messtypes = MessTypeID)FROM''附近的语法错误

  

查询错误(1054):“字段列表”中的未知列'Val'

我该如何处理。我想要MessType的值。有没有办法,我可以用SUM替换原始值中的12? ps:我不想为整个事物创建一个视图或使用IDs

3 个答案:

答案 0 :(得分:3)

使用联接:

select im.ItemID, sum(mt.MessTypeValue) as val
from ItemMess im
join MessTypes mt on im.MessTypeID = mt.MessTypeID
group by im.ItemID

没有加入:

select  im.ItemID, (select sum(mt.MessTypeValue) from MessTypes mt where im.MessTypeID = mt.MessTypeID) as val
from ItemMess im
group by im.ItemID

答案 1 :(得分:2)

我错过了什么或者你正在寻找这个吗?

select i.id, sum(messtypevalue) 
  from itemmess i
    inner join messtypes t
      on i.messtype = t.messtypeid
  group by i.itemid;

demo

这会产生以下输出:

ID  |  sum(messtypevalue)
 1  |   1

答案 2 :(得分:2)

这应该适合你:

SELECT `i`.`id`,
       Sum(`messtypevalue`)
FROM   `itemmess` AS `i`
       INNER JOIN `messtypes` AS `t`
               ON `i`.`messtype` = `t`.`messtypeid`
GROUP  BY `i`.`itemid`;