【发布时间】:2020-06-27 18:07:16
【问题描述】:
我有 table t1 存储所有请求。我有table t2,它存储与表 t1 中提出的请求相关的审计。
为了获得每个请求的最新审核,我需要在两个表上执行连接。
我可以使用以下查询执行 JOIN:
SELECT
t2.id, t1.name,
t2.Msg
FROM
requests t1
LEFT JOIN audits t2 ON t1.AuditId = t2.AuditId
ORDER BY t2.id DESC;
上面的查询返回结果如下:
id Name Msg
56895415 ABC05 Message5
56895414 ABC05 Message4
56895413 ABC05 Message3
56895303 ABC04 Message5
56895302 ABC04 Message4
56895301 ABC04 Message3
我想修改查询,以便为每个 t1.name 显示 only the last row(with highest id value)
换句话说,我的输出应该如下:
id Name Msg
56895415 ABC05 Message5
56895303 ABC04 Message5
【问题讨论】:
-
您在审计表上是否有日期/时间指示器或某种表明 msg5 是最新的行序列? (假设您想要最新的?)
-
table t1中的id提供行序列,其中id的最大值对于每个Name都有最新消息
标签: mysql join inner-join greatest-n-per-group outer-join