假设我有以下2个表:
articles_1
+----------------------------------------+
|id | title | updated_date |
+--------------------+-------------------+
|1 |article 1 | 2013/10/4 12:24:00|
+--------------------+-------------------+
|2 |article 2 | 2013/10/2 12:25:00|
+--------------------+-------------------+
|3 |article 3 | 2013/10/3 12:26:00|
+--------------------+-------------------+
articles_2
+----------------------------------------+
|id | title | updated_date |
+----------------------------------------+
|1 |article 4 | 2013/10/1 10:24:00|
+----------------------------------------+
|2 |article 5 | 2013/10/5 10:25:00|
+----------------------------------------+
|3 |article 6 | 2013/10/3 10:26:00|
+----------------------------------------+
如何从这两张表中检索最新更新的文章? 简而言之,我如何在articles_2表格中获取“第5条”的基础更新__ate(2013/10/5 10:25:00是最大更新日期)?
提前致谢!
答案 0 :(得分:3)
使用SUBSELECT和UNION语句:
SELECT id, title, updated_date, origin
FROM (
SELECT id, title, updated_date, 'articles_1' AS origin
FROM articles_1
UNION ALL
SELECT id, title, updated_date, 'articles_2' AS origin
FROM articles_2
) a
ORDER BY updated_date DESC
LIMIT 1
使用“origin”列,您可以在中知道哪个表是注册表。
答案 1 :(得分:2)
我不确定为什么你有两个包含相同类型数据的表,但你可以使用UNION
将它们放在一起,然后选择联合中的顶行:
SELECT id, title, updated_date
FROM (
SELECT id, title, updated_date
FROM articles_1
UNION ALL
SELECT id, title, updated_date
FROM articles_2
) a
ORDER BY updated_date DESC
LIMIT 1