MySQL根据条件对多个列值求和

时间:2014-03-03 13:30:03

标签: php mysql sql

我有以下架构(两个表):

**APPS**

   | ID (bigint)  | USERID (Bigint) | USAGE_START_TIME (datetime)    | 
    ------------------------------------------------------------------
   |  1           |        12       |         2013-05-03 04:42:55    |
   |  2           |        12       |         2013-05-12 06:22:45    |
   |  3           |        12       |         2013-06-12 08:44:24    |
   |  4           |        12       |         2013-06-24 04:20:56    |
   |  5           |        13       |         2013-06-26 08:20:26    |
   |  6           |        13       |         2013-09-12 05:48:27    |


**USAGE** 

   | ID (bigint)  | APPID (bigint) |   DEVICEID (bigint)  | HIGH_COUNT (bigint) |  MEDIUM_COUNT (bigint)  |
    --------------------------------------------------------------------------------------------------------
   |  1           |        1       |                  2    |       400           |                   200   |
   |  2           |        1       |                  3    |       200           |                   100   |
   |  3           |        2       |                  3    |       350           |                    40   |
   |  4           |        3       |                  4    |         2           |                   400   |
   |  5           |        4       |                  2    |         4           |                    30   |
   |  6           |        5       |                  3    |        50           |                   300   |

说明:

所以,有两个表。 现在我想找到以下内容:

  

鉴于USERID,获​​得HIGH_COUNT和&的总和MEDIUM_COUNT。算数   SUM应该注意:如果在USAGE中,则使用相同的设备   不止一次,然后记录有最新信息(基于   计算时应考虑APPS.USAGE_START_TIME)   总和。

例如:

对于上述架构,结果应为(对于userid = 12):

   | HIGH_COUNT (bigint)  | MEDIUM_COUNT (Bigint) |
    -----------------------------------------------
   |                356   |                   470 |

SQL小提琴:http://sqlfiddle.com/#!2/74ae0f

3 个答案:

答案 0 :(得分:2)

如果用户在一台设备上使用多个APPS,则此查询将使用APPSusage_start_time select a.userid , sum(u.high_count) , sum(u.medium_count) from apps a join `usage` u on u.appid = a.id join ( select u.device_id , a.userid , max(a.usage_start_time) as max_start_time from apps a join `usage` u on u.appid = a.id group by u.device_id , a.userid ) filter on filter.device_id = u.device_id and filter.userid = a.userid and filter.max_start_time = a.usage_start_time group by a.userid :{/ p>

5, 3, 4

在您的数据集中,它会为用户12选择使用行{{1}}。

See it working at SQL Fiddle.

答案 1 :(得分:1)

我无法得到你的数字,但这样的事情应该有用......

SELECT a.userid
     , SUM(u.high_count)
     , SUM(u.medium_count)
  FROM apps a
  JOIN `usage` u
    ON u.appid = a.id
  JOIN 
     ( SELECT userid
            , deviceid
            , MAX(usage_start_time) max_usage_start_time
         FROM apps a
         JOIN `usage` u
           ON u.appid = a.id
        GROUP
           BY userid
            , deviceid
     ) x
    ON x.userid = a.userid
   AND x.deviceid = u.deviceid
   AND x.max_usage_start_time = a.usage_start_time
 GROUP
    BY userid;

请注意,usage是保留字。因此,这是列(或表)的错误名称。另外,请注意您的问题与小提琴之间的不一致。

答案 2 :(得分:-2)

我认为没有机会测试它,但

SELECT SUM(HIGH_COUNT), SUM(MEDIUM_COUNT) FROM `USAGE` INNER JOIN `APPS` ON USAGE.APPID=APPS.ID WHERE APPS.USERID=$input_user_id_to_lookup

会给你你的计数。

对于yoru其他问题(家庭作业?),你没有给我们完整的架构,所以我们无法猜测你需要做什么。

同样设计那个数据库的人应该被枪杀它可怕的