gpt4 book ai didi

php - WordPress mysql 分组依据 |订购方式

转载 作者:行者123 更新时间:2023-11-29 03:03:30 25 4
gpt4 key购买 nike

$post = $wpdb->get_results("SELECT `p`.`ID`, MAX(p.post_date) as `datetime`, `p`.`post_date`, `p`.`post_content`, `p`.`post_title`, `p`.`post_status`, `p`.`post_name`, `p`.`comment_count`, `tax`.`term_taxonomy_id`, `tax`.`term_id`, `tax`.`taxonomy`, `tax`.`parent`, `rel`.`object_id`, `rel`.`term_taxonomy_id`, `t`.`term_id`, `t`.`name`, `t`.`slug`

FROM (`$wpdb->posts` AS p)

INNER JOIN `$wpdb->term_relationships` AS rel ON `rel`.`object_id` = `p`.`ID`

INNER JOIN `$wpdb->term_taxonomy` AS tax ON `tax`.`term_taxonomy_id` = `rel`.`term_taxonomy_id`

INNER JOIN `$wpdb->terms` AS t ON `t`.`term_id` = `tax`.`term_id`

WHERE `tax`.`taxonomy` = 'category'
AND `p`.`post_status` = 'publish'
AND `p`.`post_type` = 'post'

GROUP BY tax.parent

ORDER BY datetime DESC
LIMIT 4");

我需要找到每个类别的最新帖子,然后对结果进行分组,这样每个类别我只有一个最新帖子。

我用; GROUP BY tax.parent 不起作用; 按日期时间降序排序

最佳答案

您的 ID 是递增的。用那个。

select ...
from
.....
where id post_in
(select max(post_id) from table group by category)

如果你知道有多少类别,你可以使用

where post id in 
(select post_id from table where category=1 order by time desc limit 1
union
select post_id from table where category=2 order by time desc limit 1
union
select post_id from table where category=3 order by time desc limit 1
union
select post_id from table where category=4 order by time desc limit 1)

或者你可以使用参数,这会给你一个完美的结果,但查询速度很慢

select * from
(select
@rn:=if(@prv=category_id, @rn+1, 1) as rId,
@prv:=category_id as category_id,
timestamp,
other columns
from (select category_id, timestamp, other columns from ... )a
join
(select @prv:=0, @rn:=0)tmp
order by
category_id , timestamp desc) a
where rid<=1

关于php - WordPress mysql 分组依据 |订购方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19430727/

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