我正在尝试从三个单独的表中获取最新日期,每个表都有一个“ lastmodified”列。本质上,我正在尝试计算数据库的上次修改时间。
例如:
Table 1 -lastmodified column
Table 2 -lastmodified column
Table 3 -lastmodified column
我正在捆绑以下代码。我知道从每个表中获取最新最新记录的部分是错误的,但是我不确定如何更改它以使其正确。
(SELECT MAX(v) FROM (VALUES
(SELECT MAX(lastmodifieddate) FROM IMAGES),
(SELECT MAX(lastmodifieddate) FROM VIDEOS),
(SELECT MAX(lastmodifieddate) FROM DOCUMENTS)
) AS VALUE (v)) AS MaxDate)
谢谢。
答案 0 :(得分:1)
您需要使用UNION
将记录合并到来自不同表的一列中,然后从其中获取MAX
。
SELECT MAX(lastmodifieddate) AS lastmodifieddate
FROM
(
SELECT MAX(lastmodifieddate) AS lastmodifieddate FROM IMAGES
UNION
SELECT MAX(lastmodifieddate) FROM VIDEOS
UNION
SELECT MAX(lastmodifieddate) FROM DOCUMENTS
) T
答案 1 :(得分:1)
使用表中的union
合并3个表中的3个最大日期,然后获取该子查询的max()
SELECT max(date_m) as maxdateof3tables from
(
select MAX(lastmodifieddate) as date_m FROM IMAGES
union
SELECT MAX(lastmodifieddate) FROM VIDEOS
union
SELECT MAX(lastmodifieddate) FROM DOCUMENTS
) as t
答案 2 :(得分:1)
SELECT MAX(lastmodifieddate)
FROM (
SELECT lastmodifieddate from IMAGES
UNION ALL
SELECT lastmodifieddate from VIDEOS
UNION ALL
SELECT lastmodifieddate from DOCUMENTS
)T
答案 3 :(得分:1)
您Max
与Union All
一起使用
SELECT MAX(t) AS lastmodifieddate
FROM
(
SELECT MAX(lastmodifieddate) AS t FROM IMAGES
UNION ALL
SELECT MAX(lastmodifieddate) FROM VIDEOS
UNION ALL
SELECT MAX(lastmodifieddate) FROM DOCUMENTS
) Temp