0

我试图自学一些新的东西,并看看 WordPress 构建它的表格以进行修订的方式。

结构(排序)如下:

+----+------------+---------------------+------------+-----------------+-------------+
| ID | post_title |      post_date      | post_name  |   post_content  | post_parent |
+----+------------+---------------------+------------+-----------------+-------------+
|  1 | Foo        | 2012-09-20 10:00:00 | Foo        | Bar             |           0 |
|  2 | Arrrrr     | 2012-09-20 10:05:00 | Arrrr      | Pirates!        |           0 |
|  3 | Arrrrrr    | 2012-09-20 10:06:00 | revision-1 | Argh pirates    |           2 |
|  4 | Arrrrrr    | 2012-09-20 10:06:00 | revision-2 | Argh piratessss |           2 |
+----+------------+---------------------+------------+-----------------+-------------+

现在,我想做一个查询,它只给我父行(Foo 和 Arrrr)及其 ID,但带有修改后的内容。

我想出了以下查询:

SELECT original.ID, revision.post_title, revision.post_content, revision.post_name FROM wp_posts AS original
INNER JOIN wp_posts AS revision ON original.ID = revision.post_parent
WHERE original.post_status = 'publish'
AND original.post_parent = 0
ORDER BY original.ID, revision.ID DESC

这给了我以下结果:

+----+------------+---------------------+------------+-----------------+-------------+
| ID | post_title |      post_date      | post_name  |   post_content  | post_parent |
+----+------------+---------------------+------------+-----------------+-------------+
|  1 | Foo        | 2012-09-20 10:00:00 | Foo        | Bar             |           0 |
|  2 | Arrrrrr    | 2012-09-20 10:06:00 | revision-2 | Argh piratessss |           2 |
|  2 | Arrrrrr    | 2012-09-20 10:06:00 | revision-1 | Argh pirates    |           2 |
|  2 | Arrrrr     | 2012-09-20 10:05:00 | Arrrr      | Pirates!        |           0 |
+----+------------+---------------------+------------+-----------------+-------------+

但我想进一步减少到:

+----+------------+---------------------+------------+-----------------+-------------+
| ID | post_title |      post_date      | post_name  |   post_content  | post_parent |
+----+------------+---------------------+------------+-----------------+-------------+
|  1 | Foo        | 2012-09-20 10:00:00 | Foo        | Bar             |           0 |
|  2 | Arrrrrr    | 2012-09-20 10:06:00 | revision-2 | Argh piratessss |           2 |
+----+------------+---------------------+------------+-----------------+-------------+

我尝试添加DISTINCTSELECT, 并添加,GROUP BY original.ID但两者都没有给我想要的结果。

4

1 回答 1

0

虽然您无法区分一个主题的不同版本,但日期除外,您可以执行以下操作:

select * from wp_posts p join
                (
                    select max(post_date) as max_dtm, id
                    from wp_posts
                    group by if(post_parent = 0, id, post_parent)
                ) v on p.id = v.id and p.post_date = v.max_dtm; 
于 2012-09-20T09:45:19.673 回答