gpt4 book ai didi

MySQL(和 WordPress): select rows containing the highest value in one column and order by another

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

在将我的数据移至 WordPress 之前这并不难,但我不确定如何获得相同的结果:一定数量的帖子与文章属于一起(如杂志的一期),手动排序。我想获取最新一期的所有文章,并根据另一个字段对它们进行排序。

我可以用我的旧 table

select title, issue, num
from magazine
where issue = (
select max(issue)
from magazine
)
order by num

使用 WordPress,数据被分成两个表,一个用于帖子(文章),另一个用于元数据。

我可以通过以下查询获取最新一期的所有文章:

select post_title, post_date from wp_posts 
where ID in (select post_id
from wp_postmeta
where meta_key = 'myissue' and meta_value = (select max(meta_value + 0)
from wp_postmeta
where meta_key = 'myissue'));

问题是:如何按另一个自定义字段对结果进行排序?

最佳答案

如果您的查询没有返回很多结果,最简单的方法是使用临时表。查询将是这样的:

SELECT * FROM (
select post_title, post_date from wp_posts
where ID in (select post_id
from wp_postmeta
where meta_key = 'myissue' and meta_value = (select max(meta_value + 0)
from wp_postmeta
where meta_key = 'myissue'))
) tmptbl ORDER BY post_date DESC;

关于MySQL(和 WordPress): select rows containing the highest value in one column and order by another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9252760/

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