gpt4 book ai didi

mysql - SQL:如何从按类别排序的多篇文章中获取浏览量?

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:24 27 4
gpt4 key购买 nike

我使用 MySQL,我有一个代码可以从每个类别中获取最后 6 篇文章:

select a.*
from article a
where a.created >= coalesce((select a2.created
from article a2
where a2.category = a.category
order by a2.created desc
limit 5, 1
), a.created
);

现在我还需要从另一个表中获取每篇文章的总浏览量。怎么做?这不起作用:

select a.*, Count(view.*) as CountViews
from article a
where a.created >= coalesce((select a2.created
from article a2
where a2.category = a.category
order by a2.created desc
limit 5, 1
), a.created
) left join view on a.id = view.post_id;

按类别打印文章的示例:https://nedvigimostmsk.ru/

最佳答案

由于 Gordon 现在似乎处于离线状态,我将在此处发布我的编辑作为另一个答案。这应该给你你想要的顺序。

    select a.*,
(select count(*)
from views v
where a.id = v.post_id
) as num_views
from article a
where a.created >= coalesce((select a2.created
from article a2
where a2.category = a.category
order by a2.created desc
limit 5, 1
), a.created
)
ORDER BY a.created DESC;

关于mysql - SQL:如何从按类别排序的多篇文章中获取浏览量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51198638/

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