简体   繁体   中英

Select entire row based on a max value of a field - without nesting

select * 
from webOrders
where lastModifiedDate in (select max (lastModifiedDate) from webOrders) 

Is there a simpler way without nesting the selects?

Doing something like this also results in an error:

select id, amount, quantity, max(lastModifiedDate) from webOrders.

There are a couple of approaches, depending on your needs.

If you only need one row returned, you can sort on the column of interest and return the top row. For example:

select top 1 * 
from `order`
order by last_modified_date desc

if you use SQL Server or

select * 
from `order`
order by last_modified_date desc
limit 1

if you use MySQL.

If you need to get one row per group , then you do typically have to use a subquery or a join.

select TOP 1 * from [order] order by lastModifiedDate desc

You can do select

SELECT TOP 1 * FROM XXX

http://www.w3schools.com/sql/sql_top.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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