长话短说,我正试图让我的网站上次更新时间。 我网站的主要内容存储在四个表中。 每个表都有一个updated_at字段。
我可以在嵌套查询中组合它们:
SELECT (
SELECT updated_at FROM proposition ORDER BY updated_at DESC LIMIT 1
) as latest_proposition,
(
SELECT updated_at FROM product ORDER BY updated_at DESC LIMIT 1
) as latest_product,
(
SELECT updated_at FROM review ORDER BY updated_at DESC LIMIT 1
) as latest_review,
(
SELECT updated_at FROM segment ORDER BY updated_at DESC LIMIT 1
) as latest_segment
..这给了我类似的东西..
-------------------+-------------------+-------------------+-------------------
latest_proposition |latest_product |latest_review |latest_segment
-------------------+-------------------+-------------------+-------------------
2011-05-25 14:45:30|2011-05-25 14:37:28|2011-05-12 09:20:19|2011-05-12 09:20:19
-------------------+-------------------+-------------------+-------------------
1 row in set (0.00 sec)
我的问题是 - 我现在如何获得最新的这些字段?要获得整个“网站上次更新”的时间。
提前致谢!
答案 0 :(得分:4)
select
max(updated_at) as updated_at
from
(
SELECT max(updated_at) as updated_at FROM proposition
UNION
SELECT max(updated_at) FROM product
UNION
SELECT max(updated_at) FROM review
UNION
SELECT max(updated_at) FROM segment
) x
答案 1 :(得分:3)
在返回的字段上使用GREATEST()(如果您希望NULL
值,则在通过COALESCE()之后)。
答案 2 :(得分:0)
SELECT updated_at
from
(
SELECT updated_at FROM proposition ORDER BY updated_at DESC LIMIT 1
union all
SELECT updated_at FROM product ORDER BY updated_at DESC LIMIT 1
union all
SELECT updated_at FROM review ORDER BY updated_at DESC LIMIT 1
union all
SELECT updated_at FROM segment ORDER BY updated_at DESC LIMIT 1
) x
order by updated_at limit 1
答案 3 :(得分:0)
SELECT MAX(updated_at)
FROM (SELECT MAX(updated_at) FROM proposition UNION ALL
SELECT MAX(updated_at) FROM product UNION ALL
SELECT MAX(updated_at) FROM review UNION ALL
SELECT MAX(updated_at) FROM segment) x