gpt4 book ai didi

php - 从 1 :n table 的单个查询中检索最新的注释(按时间戳)

转载 作者:搜寻专家 更新时间:2023-10-30 22:22:37 26 4
gpt4 key购买 nike

假设我有两个表,users 和 notes。假设架构如下所示:

users
id, name, field2, field3

notes
id, user_id, subject, heading, body, timestamp

我想要做的是选择每个用户,以及每个用户根据时间戳发布的最新(仅 1)条评论,以显示在概览报告中。

我该怎么做呢?请注意,数据库是 MySQL。

谢谢!

最佳答案

 select users.name, notes.subject, notes.heading, notes.body
from users, notes
where users.id = notes.user_id
and notes.timestamp = (select max(timestamp) from notes where user_id = users.id)

关于php - 从 1 :n table 的单个查询中检索最新的注释(按时间戳),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1059409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com