英文:
SQL Join to the latest record in mySQL
问题
这无法解决我的问题
https://stackoverflow.com/questions/50605160/sql-join-to-the-latest-record
我想以一种方式连接表,它只从其中一个表中提取最新的记录,并计算具有特定值的read_reciept列。
以下是我的数据。
Table_One:
ID | Name |
---|---|
1 | John |
2 | Tom |
3 | Anna |
Table_Two:
ID | Visit ID | Date | read_reciept |
---|---|---|---|
1 | 2513 | 5/5/2001 | 1 |
1 | 84654 | 10/5/2012 | 1 |
1 | 454 | 4/20/2018 | 1 |
2 | 754 | 4/5/1999 | 0 |
2 | 654 | 8/8/2010 | 1 |
2 | 624 | 4/9/1982 | 1 |
3 | 7546 | 7/3/1997 | 0 |
3 | 246574 | 6/4/2015 | 1 |
3 | 15487 | 3/4/2017 | 1 |
连接后需要的结果:
ID | Name | Visit ID | Date | read_reciept |
---|---|---|---|---|
1 | John | 454 | 4/20/2018 | 3 |
2 | Tom | 654 | 8/8/2010 | 2 |
3 | Anna | 246574 | 6/4/2015 | 2 |
英文:
This does not solve my problem
https://stackoverflow.com/questions/50605160/sql-join-to-the-latest-record
I want to join tables in such a way that it fetches only the latest record from one of the tables using MySQL and also count the read_reciept columns with specific value
The following are my datas.
Table_One:
ID | Name |
---|---|
1 | John |
2 | Tom |
3 | Anna |
Table_two:
ID | Visit ID | Date | read_reciept |
---|---|---|---|
1 | 2513 | 5/5/2001 | 1 |
1 | 84654 | 10/5/2012 | 1 |
1 | 454 | 4/20/2018 | 1 |
2 | 754 | 4/5/1999 | 0 |
2 | 654 | 8/8/2010 | 1 |
2 | 624 | 4/9/1982 | 1 |
3 | 7546 | 7/3/1997 | 0 |
3 | 246574 | 6/4/2015 | 1 |
3 | 15487 | 3/4/2017 | 1 |
Results needed after Join:
ID | Name | Visit ID | Date | read_reciept |
---|---|---|---|---|
1 | John | 454 | 4/20/2018 | 3 |
2 | Tom | 654 | 8/8/2010 | 2 |
3 | Anna | 246574 | 6/4/2015 | 2 |
答案1
得分: 2
- LATERAL JOIN。只是一个文档链接,因为它的效率较低,对于MySQL来说,写起来更麻烦。
- 窗口函数:
SELECT ID, Name,VisitID, Date
FROM (
SELECT t1.ID, t1.Name, t2.VisitID, t2.Date,
row_number() over (PARTITION BY t1.ID ORDER BY t2.Date DESC) rn
FROM Table_One t1
INNER JOIN Table_Two t2 ON t2.ID = t1.ID
) t
WHERE rn = 1
如果你使用的是较旧版本,首先要表示哀悼。5.7及更旧的版本实际上已经不适合称为现代数据库了,这种情况已经存在很长时间了。但仍然可能实现。
英文:
Assuming you're up to an 8.0 release, you have two options:
- LATERAL JOIN. Just a docs link, because it's less effficient and (for MySQL) more awkward to write.
- Windowing function:
SELECT ID, Name,VisitID, Date
FROM (
SELECT t1.ID, t1.Name, t2.VisitID, t2.Date,
row_number() over (PARTITION BY t1.ID ORDER BY t2.Date DESC) rn
FROM Table_One t1
INNER JOIN Table_Two t2 ON t2.ID = t1.ID
) t
WHERE rn = 1
If you're on an older release, well condolences first of all. 5.7 and older don't even really qualify as a modern database, and hasn't for a long time. But this is still possible.
答案2
得分: 1
以下是翻译好的内容:
如果您正在使用旧版本的mysql,然后尝试以下步骤:
首先,我们需要获取最新日期:
从表Table_two中选择ID,MAX(Date)作为max_date,sum(read_reciept)作为read_reciept
按ID分组
然后,我们获取相关的Visit_ID
和sum(read_reciept):
从Table_two中选择a.ID,a.Visit_ID,t.max_date,t.read_reciept
使用INNER JOIN
选择ID,MAX(Date)作为max_date,sum(read_reciept)作为read_reciept
从表Table_two中按ID分组的子查询t
连接条件:a.ID = t.ID和a.Date = t.max_date
最后,我们使用INNER JOIN
连接我们的数据以获取期望的结果:
从Table_two中选择a.ID,b.Name,a.Visit_ID,a.Date,t.read_reciept
使用INNER JOIN
Table_One上的表b,连接条件:a.ID = b.ID
使用INNER JOIN
选择ID,MAX(Date)作为max_date,sum(read_reciept)作为read_reciept
从表Table_two中按ID分组的子查询t
连接条件:a.ID = t.ID和a.Date = t.max_date
英文:
If you are using an old version of mysql then try this :
Steps to do it :
First we need to get the latest date :
select ID, MAX(Date) as max_date, sum(read_reciept) as read_reciept
from Table_two
group by ID
Then we get the related Visit_ID
and sum(read_reciept):
SELECT a.ID, a.Visit_ID, t.max_date, t.read_reciept
FROM Table_two a
INNER JOIN (
select ID, MAX(Date) as max_date, sum(read_reciept) as read_reciept
from Table_two
group by ID
) as t on a.ID = t.ID and a.Date = t.max_date
And Finally we join our data using INNER JOIN
to get the expected result
SELECT a.ID, b.Name, a.Visit_ID, a.Date, t.read_reciept
FROM Table_two a
INNER JOIN Table_One b on a.ID = b.ID
INNER JOIN (
select ID, MAX(Date) as max_date, sum(read_reciept) as read_reciept
from Table_two
group by ID
) as t on a.ID = t.ID and a.Date = t.max_date
答案3
得分: 0
使用子查询和 row_number
获取 table_two
中的最后一行。
select Table_One.ID, name, Visit_ID, date_
from Table_One
join (select ID, Visit_ID, date_, row_number() over (partition by id order by date_ desc) rn
from Table_two) t2
on (t2.ID = Table_One.ID and t2.rn=1)
这里 row_number
根据 date
列的顺序为相同的 ID
编号行。
然后在连接时,我们只获取值为1的行,这在这种情况下表示最旧的行。
英文:
Use sub-query with row_number
to get only the last row from that table_two
.
select Table_One.ID, name, Visit_ID, date_
from Table_One
join (select ID, Visit_ID, date_, row_number() over (partition by id order by date_ desc) rn
from Table_two) t2
on (t2.ID = Table_One.ID and t2.rn=1)
Here row_number
will number row with the same ID
based on order by date
column.
Later during join we take only rows with the value being produced equal 1, which means in this case - oldest.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论