序文
Mysqlテーブルの同じフラグ(フラグ)の下にある複数のレコードの最新レコードを取得するにはどうすればよいですか?ここでの目的は、最初に時間で順序付けし、そのクエリ結果を親テーブルとして、次に親テーブルのフラグでグループ化することです。ここで、順序付けとグループ化が同じディメンションテーブルにある場合は、グループ化することに注意してください。注文前に配置する必要があります。
SQLの例
- 多次元テーブルの場合
select task.*
from task_info task
left join(
select *
from(
select *
from task_status
order by create_datetime desc) t
group by t.task_id) status on task.id = status.task_id
where
locate(status.task_status, #{
taskStatus}) > 0
order by task.create_datetime, task.id desc
- 単一次元テーブルの場合
select sub_talent_pool.*
from (select * from pool
where plan = #{
plan}
order by modify_time desc) sub_talent_pool
group by sub_talent_pool.tree_structure_id, sub_talent_pool.into_pool_time
order by sub_talent_pool.into_pool_time desc
総括する
全体として、サブクエリの逆時系列の結果が親テーブルとして使用され、外側のクエリがグループ化されて最新のレコードが取得されます。