SQL从两个表中获取最大日期
我有两个表,
USER (one row per user)
id,username,firstname,lastname,lastmodified
1,johns, John,Smith, 2009-03-01
2,andrews, Andrew,Stiller, 2009-03-03
STUDIES (multiple rows per user)
id,username,lastmodified
1,johns, 2009-01-01
1,johns, 2009-02-01
1,johns, 2009-07-01
2,andrews,2009-05-05
2,andrews,2009-04-04
我想从两个表中获取用户详细信息和最新日期:
johns,John,Smith,2009-07-01
andrews,Andrew,Stiller,2009-05-05
帮助?
I have two tables
USER (one row per user)
id,username,firstname,lastname,lastmodified
1,johns, John,Smith, 2009-03-01
2,andrews, Andrew,Stiller, 2009-03-03
STUDIES (multiple rows per user)
id,username,lastmodified
1,johns, 2009-01-01
1,johns, 2009-02-01
1,johns, 2009-07-01
2,andrews,2009-05-05
2,andrews,2009-04-04
I want to get users details and the NEWEST date from the two tables:
johns,John,Smith,2009-07-01
andrews,Andrew,Stiller,2009-05-05
Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于那些需要简单直接选择的人:
这将从 USER 获取最大日期,然后从 STUDIES 获取最大日期,然后返回这 2 个中的最大值。此外,您可能需要向内部选择添加 where 子句和一些条件以改善结果。
For those who need a simple straightforward select:
This will get the max date from USER then the max date from STUDIES, and then it will return the max of those 2. Also you may want to add where clause and some conditions to the inner selections in order to improve the result.
像这样的东西
Something like this
选择
MAX(更新日期) 为最新更新
从
(
从音频中选择更新日期
联合所有
从视频中选择更新日期
) 富
SELECT
MAX(updatedDate) as latestUpdated
FROM
(
SELECT updatedDate FROM audio
UNION ALL
SELECT updatedDate FROM videos
)foo
您需要在这里组合 MAX 和 GREATEST 函数:
MAX - 用于聚合,GREATEST - 用于两个值的最大值。
You need combination of MAX and GREATEST functions here:
MAX -- for aggregation, GREATEST -- for maximum of two values.