最大日期,找到2个表中每个用户的余额MYSQL

时间:2018-05-30 09:18:06

标签: mysql join max

我有两个包含用户余额的表。一个是每日一个表,称为每日,仅显示今天的余额,第二个表用于合并前几天用户的先前余额。

每日表格如下:

+---------+-------------------------+----------+----------+----------+
| user_id |   transactions_date     | balance1 | Balance2 | Balance3 |
+---------+-------------------------+----------+----------+----------+
| john    | 2018-05-29 10:10:00.100 |      100 |        0 |        0 |
| mike    | 2018-05-29 09:10:01.300 |      677 |        9 |      100 |
| john    | 2018-05-29 11:05:22.450 |      100 |        2 |       99 |
| philip  | 2018-05-29 10:09:40.200 |        4 |        0 |        1 |
| john    | 2018-05-29 08:21:10.090 |        6 |        0 |        0 |
| mike    | 2018-05-29 12:03:30.200 |      900 |        0 |        1 |
| mike    | 2018-05-29 10:05:00.100 |      188 |        0 |        2 |
| philip  | 2018-05-29 05:24:11.320 |       47 |        0 |        3 |
+---------+-------------------------+----------+----------+----------+

和合并表具有相同的结构,如下所示:

+---------+-------------------------+----------+----------+----------+
| user_id |   transactions_date     | balance1 | Balance2 | Balance3 |
+---------+-------------------------+----------+----------+----------+
| john    | 2018-05-24 17:10:00.200 |        9 |       11 |      198 |
| mike    | 2018-04-12 08:11:44.800 |      100 |       13 |       13 |
| philip  | 2018-05-21 12:00:59.320 |       99 |     1000 |      122 |
| jenna   | 2018-05-10 08:12:22.211 |     2000 |        0 |       11 |
| jenna   | 2018-05-11 10:09:10.199 |     2999 |        1 |        1 |
| paul    | 2018-04-01 12:12:11.191 |      888 |      100 |      100 |
+---------+-------------------------+----------+----------+----------+

我的查询需要为每个用户找到MAX date_transaction,所以我应该需要一个结果:

+---------+-------------------------+----------+----------+----------+
| user_id |   transactions_date     | balance1 | Balance2 | Balance3 |
+---------+-------------------------+----------+----------+----------+
| john    | 2018-05-29 11:05:22.450 |      100 |        2 |       99 |
| mike    | 2018-05-29 12:03:30.200 |      900 |        0 |        1 |
| philip  | 2018-05-29 10:09:40.200 |        4 |        0 |        1 |
| jenna   | 2018-05-11 10:09:10.199 |     2999 |        1 |        1 |
| paul    | 2018-04-01 12:12:11.191 |      888 |      100 |      100 |
+---------+-------------------------+----------+----------+----------+

我可以设法通过此查询获取每日表的结果,以获取用户的最大日期:

SELECT
    t2.user_id ,
    t2.balance1 ,
    t2.balance2,
    t2.balance3
FROM
    (
        SELECT
            user_id ,
            MAX(transactions_date) AS max
        FROM
            daily
        GROUP BY
            user_id
    ) t1 ,
    (
        SELECT
            user_id ,
            balance1 ,
            balance2 ,
            balance3 ,
            transactions_date
        FROM
            daily
    ) t2
WHERE
    t1.user_id = t2.user_id
AND t1.max = t2.transactions_date

我不知道是否有更好的方法每天首先在桌面上执行此过程。然后我试图通过user_id加入每日和合并表来获得max date_transaction而没有结果。

我添加了Mysqlfiddle

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

SELECT 
    A.*
FROM
    (SELECT * FROM daily
     UNION ALL
     SELECT * FROM consolidation) A 
JOIN
    (SELECT 
        T.user_id, MAX(transactions_date) max_user_trans_date
     FROM
        (SELECT * FROM daily
         UNION ALL
         SELECT * FROM consolidation) T
     GROUP BY T.user_id) B
ON A.user_id=B.user_id AND A.transactions_date=B.max_user_trans_date; 

run on SQL Fiddle