0

我有一个查询,它返回一个数据库表中与给定条件匹配的所有帖子,但我正在寻找一种方法,可以从每个'post_type'. 目前,查询正在选择每个匹配的帖子,我不得不限制'post_type'PHP 中每个帖子的数量,这不是特别有效。

这可以做到吗?谢谢。

SELECT ID, post_title, post_type
FROM wp_posts
WHERE 1=1
AND post_status = "publish"
AND (
    post_type IN (
        "staff"
    )
    AND post_name LIKE "%The%"
)
OR (
    post_type NOT IN (
        "staff",
        "Attachment"
    )
    AND (
        post_name LIKE "%The%"
        OR post_title LIKE "%The%"
    )
)
ORDER BY post_type, post_name ASC
4

1 回答 1

1

此解决方案将选择五个最近(基于id)的帖子post_type

SELECT     a.id, a.post_title, a.post_type
FROM       wp_posts a
INNER JOIN wp_posts b ON a.post_type = b.post_type AND a.id <= b.id
WHERE      a.post_status = 'publish' AND
           (a.post_type = 'staff' AND a.post_name LIKE '%The%') OR
           (a.post_type NOT IN ('staff', 'Attachment') AND (a.post_name LIKE '%The%' OR a.post_title LIKE '%The%'))
GROUP BY   a.id, a.post_title, a.post_type
ORDER BY   a.post_type, a.post_name
HAVING     COUNT(1) <= 5
于 2012-07-17T10:13:17.250 回答