如何从数据库中获取最近的 10 行?

How can I get the 10 most recent rows from a database?

我想在 table 中选择一个 id,按日期排序并只显示 10 个最新条目。

我已经尝试过以下命令:

SELECT * FROM weather 
  WHERE DATUM = (SELECT MAX(DATUM) WHERE ID='0')

您似乎想要过滤、排序和限制:

select *
from weather
where id = 0          -- filter on the given "id"
order by datum desc   -- sort by most recent date
limit 10              -- keep the 10 most recent only

SELECT TOP 10 *
FROM weather
WHERE DATUM = (SELECT MAX(DATUM)
               FROM orders
               WHERE ID='0')
order by DATUM DESC

希望对大家有所帮助。

此事件具体与任何日期和时间范围相关。因此,您可以轻松地为您的应用程序进行定制。

/* mySQL doesn't permit to use limit clause on sub query at where clause statement.
   Therefore we are using join clause as an alternative.
*/

select w.*
from weather w
inner join (
    select min(DATUM) fromDate, max(DATUM) toDate
    from (
        select DATUM from ORDERS where ID = '0'
        order by DATUM desc limit 10
    ) t
) o
on w.DATUM between o.fromDate and o.toDate
order by w.DATUM desc

/* Transaction table with limited rows option */

select w.*
from weather w
inner join (
    select min(DATUM) fromDate, max(DATUM) toDate
    from orders where ID = '0'
) o
on w.DATUM between o.fromDate and o.toDate
order by w.DATUM desc limit 10