gpt4 book ai didi

mysql - 如何从列中获取唯一值,同时返回最新的非唯一值

转载 作者:行者123 更新时间:2023-11-29 22:14:32 27 4
gpt4 key购买 nike

我有一组这样的表:

post_id | post_content | page_id     
1 etc... 7
2 text... 5

changelog_id | post_id | page_id |   changelog_date    | changelog_type
1 2 5 02/02/2015 11:05 3
2 2 5 02/02/2015 11:15 2
3 1 7 03/01/2015 18:15 3

我想返回:

select distinct post_id, post_content, changelog_type, changelog_date
from posts p
inner join logs l on l.post_id = p.post_id
where p.page_id = 5

但是不同的不会有效,因为changelog_date不是唯一的。 我希望我的返回是这样的:

 post_id| post_content |  changelog_type   | changelog_date
2 text... 02/02/2015 11:15 2

具有 page_id 的 post_id 的最新条目,并且每个 post_id 只有一个结果。

我尝试了 GROUP BY,但我得到:

列“page.pageId”在选择列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中。

在我的实际使用中,我有 6 个表可以从中获取信息,并且我正在使用内连接来从它们中获取信息。我只需要通过为每个 post_id 实例只获取一个来减少返回。

最佳答案

试试这个:-

select distinct post_id, post_content, changelog_type, MAX(changelog_date)
from posts p
inner join logs l on l.post_id = p.post_id
where p.page_id = 5
group by post_id, post_content, changelog_type

关于mysql - 如何从列中获取唯一值,同时返回最新的非唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31319915/

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