【发布时间】:2019-05-25 15:11:05
【问题描述】:
对于这样含糊的标题,我深表歉意。
背景
我有一个 MySQL 表,其中包含 Wordpress 博客文章。该表包含帖子及其翻译,例如:
----------------------------------------------------
|post_name|post_type|post_status|post_date |
----------------------------------------------------
|My post | post | publish |2019-05-18 05:00:20 |
----------------------------------------------------
| Mi post | post | publish |2019-05-18 05:00:20 |
----------------------------------------------------
每个帖子和每个翻译都是表格中独立的帖子条目。我正在尝试整理一个 SQL 语句,该语句将为我提供一个帖子列表及其翻译名称。翻译之间唯一的共同点是帖子的日期完全相同。
当前 SQL 语句
到目前为止,我写道:
SELECT t1.post_name, t2.post_name, t1.post_date
FROM `posts` t1 LEFT JOIN `posts` t2 ON t1.post_date = t2.post_date
WHERE t1.post_status='publish' and
t1.post_type='post' and
t1.post_name<>t2.post_name
ORDER BY t1.post_date DESC
输出
-----------------------------------------
|post_name|post_name| post_date |
-----------------------------------------
|My post | Mi post | 2019-05-18 05:00:20 |
-----------------------------------------
| Mi post | My post | 2019-05-18 05:00:20 |
-----------------------------------------
如您所见,我获得了两次条目(这是相当合乎逻辑的)。那么,我怎样才能摆脱重复的条目呢?
谢谢
【问题讨论】:
-
不管怎样,如果我假设所有翻译的帖子 ID 都比原始帖子大,那么我可以添加一个约束
where t1.id>t2.id并消除重复项。跨度> -
翻译是否超过2个?
-
不 - 只有 2 个 - 并且原始帖子的日期永远不会相同。
-
通过时间戳识别帖子是个坏主意。谁设计了该架构?
-
根本没有人 - 这只是一个 hack。系统不是这样设计的。