Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL按最近的评论或最近发布的帖子排序_Mysql_Sql_Select_Sql Order By - Fatal编程技术网

MySQL按最近的评论或最近发布的帖子排序

MySQL按最近的评论或最近发布的帖子排序,mysql,sql,select,sql-order-by,Mysql,Sql,Select,Sql Order By,如何对帖子进行排序,使最新活动位于顶部? # Schema not including all info, including FKs CREATE TABLE post( id int unsigned primary key auto_increment, msg text, created datetime )ENGINE=InnoDb; CREATE TABLE comment( id int unsigned primary key auto_incremen

如何对帖子进行排序,使最新活动位于顶部?

# Schema not including all info, including FKs
CREATE TABLE post(
   id int unsigned primary key auto_increment,
   msg text,
   created datetime
)ENGINE=InnoDb;

CREATE TABLE comment(
   id int unsigned primary key auto_increment,
   post_id int unsigned,
   msg text,
   created datetime
)ENGINE=InnoDb;
我想按最近发布的帖子排序,新发布的帖子显然比以前发布的帖子更新,但是一篇有最近评论的老帖子仍然符合最近发布的条件

第一次尝试

# Selecting '*' for simplicity in this example
select *
from post p
left join comment c on c.post_id = p.id
group by p.id
order by c.created desc, p.created desc
select *, if(c.id is null, p.created, c.created) as recency
from post p
left join comment c on c.post_id = p.id
group by p.id
order by recency desc
这不起作用,因为新帖子是在有评论的旧帖子之后排序的

第二次尝试

# Selecting '*' for simplicity in this example
select *
from post p
left join comment c on c.post_id = p.id
group by p.id
order by c.created desc, p.created desc
select *, if(c.id is null, p.created, c.created) as recency
from post p
left join comment c on c.post_id = p.id
group by p.id
order by recency desc
不起作用,因为如果一篇文章有多条注释,则“最近性”将匹配第一行的创建值,这是最早的注释

*是否有办法按p.id进行分组(因此每个帖子只选择一个副本),但各组内的排序是按c.date desc进行的,而查询顺序是按Recenty进行的?我想不出一种方法来做到这一点,而不可能在帖子中添加一个更新的字段,每当有回复发布时,我都会给它写信

谢谢

这应该可以做到:

SELECT p.id
FROM post p
    LEFT JOIN comment c on c.post_id = p.id
GROUP BY p.id
ORDER BY COALESCE(GREATEST(p.created, MAX(c.created)), p.created) DESC
如果我们假设评论总是比帖子旧,我们可以简化:

SELECT p.id
FROM post p
    LEFT JOIN comment c on c.post_id = p.id
GROUP BY p.id
ORDER BY COALESCE(MAX(c.created), p.created) DESC

这是一种工作方式,但顺序相反-这可以通过在查询末尾添加DESC来解决。不过,还是不太管用。很漂亮。谢谢我不熟悉合并函数。再次感谢!!这完全符合我的实际情况。非常感谢你!今晚我学到了一些新东西。虽然没有保留,
comment
在MySQL中是一个公认的关键字,所以我有点担心将其用作表/列标识符