简体   繁体   English

当max在其他列上时获取id值

[英]get id value when max is on other column

is this the best way to get the id value of the most recent date? 这是获取最近日期的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 )

You already have a good way there. 你已经有了一个好方法。 I'd watch out for ties: 我要注意关系:

select top id from table1 where entrydate = ( select MAX(entrydate) from table1 )

This, of course, assuming you are using SQL Server. 当然,这是假设您使用的是SQL Server。

Assuming you're using SQL-Server, you can use ORDER BY and then take one row: 假设您正在使用SQL-Server,您可以使用ORDER BY然后取一行:

SELECT TOP 1 id
FROM table
ORDER BY entrydate DESC

In MySql it is LIMIT : 在MySql中它是LIMIT

SELECT id
FROM table
ORDER BY entrydate DESC
LIMIT 1

In Oracle: 在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

Not exactly, you want to do this: 不完全是,你想这样做:

For SQL Server: 对于SQL Server:

SELECT TOP 1 id, MAX(entrydate) FROM table1 GROUP BY id

For MySQL: 对于MySQL:

SELECT id, MAX(entrydate) FROM table1 GROUP BY id LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM