[英]GROUP BY with MAX(timestamp) in JOIN table
This is my first post here and already asking for help :/. 这是我在这里的第一篇文章,已经在寻求帮助了:/。
I'm really struggling to create this report, I've been learning mySQL for the last couple of months and I need help. 我真的很难创建此报告,最近几个月我一直在学习mySQL,并且需要帮助。
I have 4 tables that have foreign keys and In summary I need to retrieve the value of 2 of those tables where the most recent create date: 我有4个具有外键的表,总之,我需要检索这些表中最近创建日期的2个值:
table1 表格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
table2 表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
table3 表3
ID foreignkey1 foreignkey2
1 1372612 3112602
2 1373280 3099667
table4 表4
ID foreignkey1 country
1 1372612 Mordor
2 1373280 Mordor
What I need is a query that retrieves something like this: 我需要的是一个检索如下内容的查询:
foreignkey1 foreignkey2 date2 date3 country
3112602 1372612 7/20/17 7/20/17 Mordor
3099667 1373280 7/16/17 7/16/17 Mordor
I need to join both foreign keys using table3 and retrieve both date2 and date3 WHERE created is the most recent date on each table. 我需要使用table3联接两个外键,并同时检索创建在每个表上的最新日期为date2和date3的日期。
I tried something like this: 我尝试过这样的事情:
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;
Unfortunately I haven't had success in this mission, I need help :( 不幸的是,我在这次任务中没有取得成功,我需要帮助:(
Thanks! 谢谢!
Start by getting the data with the max(created) date from each table and then join to that table. 首先从每个表中获取具有最大(创建)日期的数据,然后加入该表。 For example:
例如:
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;
I might be off by the joins as I did not recreate the data on my PC, but that's one way to go about it... 我可能没有参加联接,因为我没有在PC上重新创建数据,但这是解决问题的一种方法...
Could be you have some problem with on clause 可能是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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.