[英]get id value when max is on other column
这是获取最近日期的id值的最佳方法吗?
table1
id,entrydate
1,8/23/2012
2,8/24/2012
3,8/23/2012
select id from table1 where entrydate = ( select MAX(entrydate) from table1 )
你已经有了一个好方法。 我要注意关系:
select top id from table1 where entrydate = ( select MAX(entrydate) from table1 )
当然,这是假设您使用的是SQL Server。
假设您正在使用SQL-Server,您可以使用ORDER BY
然后取一行:
SELECT TOP 1 id
FROM table
ORDER BY entrydate DESC
在MySql中它是LIMIT
:
SELECT id
FROM table
ORDER BY entrydate DESC
LIMIT 1
在Oracle中:
SELECT id
FROM (SELECT id FROM table ORDER BY entrydate DESC)
WHERE ROWNUM = 1
SELECT id FROM table1 ORDER BY entrydate DESC LIMIT 1
您应该可以执行SELECT id FROM table1 ORDER BY entrydate DESC LIMIT 1
不完全是,你想这样做:
对于SQL Server:
SELECT TOP 1 id, MAX(entrydate) FROM table1 GROUP BY id
对于MySQL:
SELECT id, MAX(entrydate) FROM table1 GROUP BY id LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.