JOIN表中具有MAX(时间戳)的GROUP BY

时间:2017-07-03 18:19:52

标签: mysql sql

这是我在这里的第一篇帖子,已经在寻求帮助:/。

我真的很难创建这份报告,我在过去几个月里一直在学习mySQL,我需要帮助。

我有4个具有外键的表,总而言之,我需要检索最近创建日期的那些表中的2个值:

表1

ID  created         Date2   Amount   foreignkey1
1   6/14/17 12:08   7/13/17  0      3112602
2   6/14/17 12:08   7/20/17  0      3112602
3   6/14/17 12:08   7/20/17  1000   3112602
4   6/14/17 12:18   7/20/17  3631   3112602
5   6/14/17 13:06   7/20/17  6483   3112602
6   6/2/17 10:06    7/1/17   0      3099667
7   6/2/17 10:06    7/16/17  0      3099667
8   6/30/17 6:54    7/16/17  10163  3099667
9   6/30/17 6:55    7/16/17  10163  3099667

表2

ID  Created        Date3    foreignkey2
1   10/4/11 17:46   NULL    1373280
2   2/6/12 21:10    NULL    1373280
3   11/16/12 20:23  NULL    1373280
4   3/19/13 12:03   NULL    1373280
5   5/27/17 7:48    NULL    1373280
6   5/30/17 13:56   NULL    1373280
7   5/30/17 14:32   7/1/17  1373280
8   6/2/17 8:06     7/16/17 1373280
8   3/19/09 16:34   NULL    1372612
8   1/18/17 18:12   NULL    1372612
8   6/14/17 6:24    7/20/17 1372612

表3

ID  foreignkey1 foreignkey2
1   1372612     3112602
2   1373280     3099667

表4

ID  foreignkey1 country
1   1372612      Mordor
2   1373280      Mordor

我需要的是一个检索如下内容的查询:

foreignkey1 foreignkey2  date2       date3    country
3112602     1372612      7/20/17     7/20/17   Mordor
3099667     1373280      7/16/17     7/16/17   Mordor

我需要使用table3连接两个外键并检索date2和date3 WHERE created是每个表上的最新日期。

我试过这样的事情:

SELECT tb1.foreignkey1, tb2.foreignkey2, tb1.date2, tb2.date3, tb3.country FROM table1 tb1
    LEFT JOIN table3 tb3 on tb1.foreignkey1=tb3.foreignkey1
    LEFT JOIN table2 tb2 on tb2.foreignkey2=tb3.foreingkey2
    LEFT JOIN table4 tb4 on tb2.foreignkey1=tb4.foreingkey4
WHERE tb1.created>"2017-06-01" AND tb4.country="Mordor" AND tb1.created IN (SELECT MAX(ca.created)) AND tb2.created = (SELECT MAX(tb2.created))  AND tb1.date2 >"2017-06-01" AND tb2.date3 >"2017-06-01" GROUP BY cu.ID;

不幸的是,我没有成功完成这项任务,我需要帮助:(

谢谢!

2 个答案:

答案 0 :(得分:0)

首先从每个表中获取具有最大(创建)日期的数据,然后加入该表。例如:

SELECT  foreignkey1 ,
    foreignkey2 ,
    date2 ,
    date3 ,
    country
FROM    ( SELECT    MAX(id) AS id ,
                foreignkey1 ,
                MAX(created) AS created ,
                MAX(date2)
      FROM      table1
      GROUP BY  foreignkey1
    ) t1
    JOIN table3 t3 ON t3.foreignkey1 = t1.foreignkey1
    JOIN ( SELECT   MAX(id) AS id ,
                    foreignkey2 ,
                    MAX(date3) ,
                    MAX(created) AS created
           FROM     table2
           GROUP BY foreignkey2
         ) t2 ON t2.foreignkey2 = t3.foreignkey2
    JOIN table4 t4 ON t4.foreignkey1 = t1.foreignkey1;

由于我没有在我的电脑上重新创建数据,我可能会加入,但这是一种方法...

答案 1 :(得分:0)

你可能对on

有一些问题
    SELECT 
        tb1.foreignkey1
        , tb2.foreignkey2
        , tb1.date2
        , tb2.date3
        , tb4.country 
    from table1 tb1
    left join table3 tb3 on tb1.foreignkey1=tb3.foreignkey2
    left join table2 tb2 on tb2.foreignkey2=tb3.foreingkey1
    left join table4 tb4 on tb3.foreignkey1=tb4.foreingkey1
    WHERE tb1.created>"2017-06-01" 
    AND tb4.country="Mordor" 
    AND tb1.created IN (SELECT MAX(ca.created)) 
    AND tb2.created = (SELECT MAX(tb2.created))  
    AND tb1.date2 >"2017-06-01" AND tb2.date3 >"2017-06-01" 
    GROUP BY cu.ID;