我试图自学一些新的东西,并看看 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 |
+----+------------+---------------------+------------+-----------------+-------------+
我尝试添加DISTINCT
到SELECT
, 并添加,GROUP BY original.ID
但两者都没有给我想要的结果。