简体   繁体   English

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

[英]MySQL Sum multiple column values with conditions

I have the following schema (two tables): 我有以下架构(两个表):

**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   |

Explanation: 说明:

So, there are two tables. 所以,有两个表。 Now I want to find the following: 现在我想找到以下内容:

Given a USERID, Get sum of HIGH_COUNT & MEDIUM_COUNT. 给定USERID,获​​取HIGH_COUNT和MEDIUM_COUNT之和。 While counting the SUM it should be taken care that: If in USAGE, same device is used more than once, then the record which has the latest info (based on APPS.USAGE_START_TIME), should be considered while calculating the sum. 在计算SUM时,应注意:如果在USAGE中,同一设备被多次使用,则在计算总和时应考虑具有最新信息的记录(基于APPS.USAGE_START_TIME)。

For ex: 例如:

For above schema, result should be (for userid=12) : 对于上面的模式,结果应该是(对于userid = 12):

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

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

If a user uses multiple APPS on one device, this query will use the APPS row with the highest usage_start_time : 如果用户在一台设备上使用多个APPS ,则此查询将使用具有最高usage_start_timeAPPS行:

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

In your dataset, it will select usage rows 5, 3, 4 for user 12 . 在你的数据集,它会选择使用排5, 3, 4用户12

See it working at SQL Fiddle. 看到它在SQL Fiddle工作。

I can't quite get your numbers, but something like this should work... 我无法得到你的数字,但这样的事情应该有效......

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;

Note that usage is a reserved word. 请注意, usage是保留字。 Therefore, this is a bad name for a column (or a table). 因此,这是列(或表)的错误名称。 Also, note inconsistencies between your question and your fiddle. 另外,请注意您的问题与小提琴之间的不一致。

I think not had chance to test it but 我认为没有机会测试它,但是

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

will give you your counts. 会给你你的数量。

For yoru other question (homework?) you didn't give us the full schema so we can't guess what you need doing. 对于yoru其他问题(家庭作业?)你没有给我们完整的架构,所以我们无法猜测你需要做什么。

Also whoever designed that db should be shot its horrible 同样设计那个数据库的人应该被击中它的可怕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM