获取最近标记的记录
Get most recent flagged record
我有以下 MySQL (v5.7) table:
ID
PersonID
Featured
CreatedAt
1743455
54924
0
2021-10-21 12:15:54
1743460
54924
1
2021-10-21 12:30:38
1743465
54924
0
2021-10-21 12:52:05
1743467
54924
0
2021-10-21 12:54:58
1743471
54924
0
2021-10-21 13:10:22
1743472
54924
1
2021-10-21 13:23:27
1743473
54924
0
2021-10-21 13:42:00
1743474
54924
0
2021-10-21 13:43:50
1743475
54924
0
2021-10-21 13:47:08
1743476
54924
0
2021-10-21 13:48:36
1743484
54924
1
2021-10-21 16:31:03
我想要一个查询来创建一个标记最新特写照片的列
预期结果:
ID
PersonID
Featured
CreatedAt
MostRecentFeatured
1743455
54924
0
2021-10-21 12:15:54
0
1743460
54924
1
2021-10-21 12:30:38
0
1743465
54924
0
2021-10-21 12:52:05
0
1743467
54924
0
2021-10-21 12:54:58
0
1743471
54924
0
2021-10-21 13:10:22
0
1743472
54924
1
2021-10-21 13:23:27
0
1743473
54924
0
2021-10-21 13:42:00
0
1743474
54924
0
2021-10-21 13:43:50
0
1743475
54924
0
2021-10-21 13:47:08
0
1743476
54924
0
2021-10-21 13:48:36
0
1743484
54924
1
2021-10-21 16:31:03
1
我该怎么做?
我尝试使用子查询,但它减慢了我的查询速度。
在 mysql 8+ :
select * , case when row_number() over (partition by PersonId order by CreatedAt desc) = 1 then 1 else 0 end as MostRecentFeatured
from table1
年长 mysql :
select a.*, case when b.personId is not null then 1 else 0 end as mostrecent
from table a
left join (
select PersonId, max(CreatedAt) as CreatedAt
from table
group by PersonId
) b on a.CreatedAt = b.CreatedAt
and a.PersonId = b.PersonId
我有以下 MySQL (v5.7) table:
ID | PersonID | Featured | CreatedAt |
---|---|---|---|
1743455 | 54924 | 0 | 2021-10-21 12:15:54 |
1743460 | 54924 | 1 | 2021-10-21 12:30:38 |
1743465 | 54924 | 0 | 2021-10-21 12:52:05 |
1743467 | 54924 | 0 | 2021-10-21 12:54:58 |
1743471 | 54924 | 0 | 2021-10-21 13:10:22 |
1743472 | 54924 | 1 | 2021-10-21 13:23:27 |
1743473 | 54924 | 0 | 2021-10-21 13:42:00 |
1743474 | 54924 | 0 | 2021-10-21 13:43:50 |
1743475 | 54924 | 0 | 2021-10-21 13:47:08 |
1743476 | 54924 | 0 | 2021-10-21 13:48:36 |
1743484 | 54924 | 1 | 2021-10-21 16:31:03 |
我想要一个查询来创建一个标记最新特写照片的列
预期结果:
ID | PersonID | Featured | CreatedAt | MostRecentFeatured |
---|---|---|---|---|
1743455 | 54924 | 0 | 2021-10-21 12:15:54 | 0 |
1743460 | 54924 | 1 | 2021-10-21 12:30:38 | 0 |
1743465 | 54924 | 0 | 2021-10-21 12:52:05 | 0 |
1743467 | 54924 | 0 | 2021-10-21 12:54:58 | 0 |
1743471 | 54924 | 0 | 2021-10-21 13:10:22 | 0 |
1743472 | 54924 | 1 | 2021-10-21 13:23:27 | 0 |
1743473 | 54924 | 0 | 2021-10-21 13:42:00 | 0 |
1743474 | 54924 | 0 | 2021-10-21 13:43:50 | 0 |
1743475 | 54924 | 0 | 2021-10-21 13:47:08 | 0 |
1743476 | 54924 | 0 | 2021-10-21 13:48:36 | 0 |
1743484 | 54924 | 1 | 2021-10-21 16:31:03 | 1 |
我该怎么做?
我尝试使用子查询,但它减慢了我的查询速度。
在 mysql 8+ :
select * , case when row_number() over (partition by PersonId order by CreatedAt desc) = 1 then 1 else 0 end as MostRecentFeatured
from table1
年长 mysql :
select a.*, case when b.personId is not null then 1 else 0 end as mostrecent
from table a
left join (
select PersonId, max(CreatedAt) as CreatedAt
from table
group by PersonId
) b on a.CreatedAt = b.CreatedAt
and a.PersonId = b.PersonId