从 WordPress 数据库中检索每个 post 的最新 10 张图像不起作用。可能的修复?
Retrieving latest 10 images per post from WordPress DB doesn't work. Possible fix?
我需要使用自定义 MySql 查询从 WordPress 数据库 (MySQL 5.0) 的帖子 table 中检索每个 post 的最新 10 张图像,所以我写了这个:
SELECT p.*, COUNT( p.post_parent ) AS counter
FROM wp_posts AS p
LEFT OUTER JOIN wp_posts AS p_temp
ON p.post_parent = p_temp.post_parent AND
p.post_date < p_temp.post_date
WHERE p.post_type = 'attachment' AND
p.post_mime_type LIKE 'image/%' AND
p.post_parent > 0
GROUP BY p.ID
HAVING counter <= 10
ORDER BY p.post_parent, p.post_date DESC
问题是我得到的 counter
不是从每个 post_parent
组的 1 开始的,也不是连续的。所以结果不可靠。
测试数据库的一些示例数据return是我:
ID post_date post_parent post_type counter
---------------------------------------------------------------
502 2020-03-02 17:42:03 463 attachment 2
474 2020-01-28 18:41:55 463 attachment 5
933 2020-10-26 09:28:54 497 attachment 1
932 2020-10-26 09:28:34 497 attachment 2
500 2020-03-02 17:29:08 497 attachment 6
499 2020-03-02 17:28:55 497 attachment 7
498 2020-03-02 17:28:43 497 attachment 8
我如何修复查询,以便它为 post_parent
的每个值(附加图像的 post 的 ID)最多 return 10 个结果(图像) )?
如果我没看错,您需要每个 post_parent
10 行,其中排序由列 counter
定义。这是一种使用相关子查询进行过滤的方法:
select p.*
from wp_posts p
where
p.post_type = 'attachment' and p.post_mime_type LIKE 'image/%' and p.post_parent > 0
and p.counter <= (
select p.counter
from wp_posts p1
where p1.post_type = 'attachment' and p1.post_mime_type LIKE 'image/%' and p1.post_parent = p.post_parent
order by p1.counter
limit 9, 1
)
这为您提供了每个 post_parent
的“前”10 行。如果您想要最新的 10 个,那么您可以将 <=
更改为 >=
,并将 order by p1.counter limit 9, 1
更改为 order by p1.counter desc limit 9, 1
。
这很棘手,尤其是因为有些帖子可能没有十行。因此,基本思想类似于 GMB 的回答,但同时考虑到没有 9 行(并使用正确的列):
select p.*
from wp_posts p
where p.post_type = 'attachment' and
p.post_mime_type like 'image/%' and
p.post_parent > 0 and
p.post_date >= coalesce( (select p2.post_date
from wp_posts p2
where p2.post_type = 'attachment' and
p2.post_mime_type like 'image/%' and
p2.post_parent > 0
order by p2.post_date desc
limit 9, 1
), p.post_date
);
编辑:
如果你想要这个 per post_parent
使用关联子句:
p.post_date >= coalesce( (select p2.post_date
from wp_posts p2
where p2.post_type = 'attachment' and
p2.post_mime_type like 'image/%' and
p2.post_parent = p.post_parent
order by p2.post_date desc
limit 9, 1
), p.post_date
);
我需要使用自定义 MySql 查询从 WordPress 数据库 (MySQL 5.0) 的帖子 table 中检索每个 post 的最新 10 张图像,所以我写了这个:
SELECT p.*, COUNT( p.post_parent ) AS counter
FROM wp_posts AS p
LEFT OUTER JOIN wp_posts AS p_temp
ON p.post_parent = p_temp.post_parent AND
p.post_date < p_temp.post_date
WHERE p.post_type = 'attachment' AND
p.post_mime_type LIKE 'image/%' AND
p.post_parent > 0
GROUP BY p.ID
HAVING counter <= 10
ORDER BY p.post_parent, p.post_date DESC
问题是我得到的 counter
不是从每个 post_parent
组的 1 开始的,也不是连续的。所以结果不可靠。
测试数据库的一些示例数据return是我:
ID post_date post_parent post_type counter
---------------------------------------------------------------
502 2020-03-02 17:42:03 463 attachment 2
474 2020-01-28 18:41:55 463 attachment 5
933 2020-10-26 09:28:54 497 attachment 1
932 2020-10-26 09:28:34 497 attachment 2
500 2020-03-02 17:29:08 497 attachment 6
499 2020-03-02 17:28:55 497 attachment 7
498 2020-03-02 17:28:43 497 attachment 8
我如何修复查询,以便它为 post_parent
的每个值(附加图像的 post 的 ID)最多 return 10 个结果(图像) )?
如果我没看错,您需要每个 post_parent
10 行,其中排序由列 counter
定义。这是一种使用相关子查询进行过滤的方法:
select p.*
from wp_posts p
where
p.post_type = 'attachment' and p.post_mime_type LIKE 'image/%' and p.post_parent > 0
and p.counter <= (
select p.counter
from wp_posts p1
where p1.post_type = 'attachment' and p1.post_mime_type LIKE 'image/%' and p1.post_parent = p.post_parent
order by p1.counter
limit 9, 1
)
这为您提供了每个 post_parent
的“前”10 行。如果您想要最新的 10 个,那么您可以将 <=
更改为 >=
,并将 order by p1.counter limit 9, 1
更改为 order by p1.counter desc limit 9, 1
。
这很棘手,尤其是因为有些帖子可能没有十行。因此,基本思想类似于 GMB 的回答,但同时考虑到没有 9 行(并使用正确的列):
select p.*
from wp_posts p
where p.post_type = 'attachment' and
p.post_mime_type like 'image/%' and
p.post_parent > 0 and
p.post_date >= coalesce( (select p2.post_date
from wp_posts p2
where p2.post_type = 'attachment' and
p2.post_mime_type like 'image/%' and
p2.post_parent > 0
order by p2.post_date desc
limit 9, 1
), p.post_date
);
编辑:
如果你想要这个 per post_parent
使用关联子句:
p.post_date >= coalesce( (select p2.post_date
from wp_posts p2
where p2.post_type = 'attachment' and
p2.post_mime_type like 'image/%' and
p2.post_parent = p.post_parent
order by p2.post_date desc
limit 9, 1
), p.post_date
);