简体   繁体   English

MySQL每天选择最后一行

[英]MySQL select last row each day

Trying to select last row each day. 尝试每天选择最后一行。

This is my (simplified, more records in actual table) table: 这是我的(简化的,实际表中有更多记录)表:

+-----+-----------------------+------+
| id  |       datetime        | temp |
+-----+-----------------------+------+
|  9  | 2017-06-05 23:55:00   | 9.5  |
|  8  | 2017-06-05 23:50:00   | 9.6  |
|  7  | 2017-06-05 23:45:00   | 9.3  |
|  6  | 2017-06-04 23:55:00   | 9.4  |
|  5  | 2017-06-04 23:50:00   | 9.2  |
|  4  | 2017-06-05 23:45:00   | 9.1  |
|  3  | 2017-06-03 23:55:00   | 9.8  |
|  2  | 2017-06-03 23:50:00   | 9.7  |
|  1  | 2017-06-03 23:45:00   | 9.6  |
+-----+-----------------------+------+

I want to select row with id = 9, id = 6 and id = 3. 我想选择id = 9,id = 6和id = 3的行。

I have tried this query: 我已经尝试过以下查询:

SELECT MAX(datetime) Stamp
     , temp 
  FROM weatherdata 
 GROUP 
    BY YEAR(DateTime)
     , MONTH(DateTime)
     , DAY(DateTime) 
 order 
    by datetime desc 
 limit 10;

But datetime and temp does not match. 但是日期时间和温度不匹配。

Kind Regards 亲切的问候

Here's one way, which gets the MAX date per day and then uses it in the INNER query to get the other fields: 这是一种获取每天MAX日期,然后在INNER查询中使用它来获取其他字段的方法:

SELECT * 
FROM test
WHERE `datetime` IN (
  SELECT MAX(`datetime`) 
  FROM test
  GROUP BY DATE(`datetime`)
 );

Here's the SQL Fiddle . 这是SQL Fiddle

If your rows are always inserted and never updated, and if id is an autoincrementing primary key, then 如果您的行总是插入而从未更新,并且id是自动递增的主键,则

SELECT w.*
  FROM weatherdata w
  JOIN (   SELECT MAX(id) id
             FROM weatherdata
            GROUP BY DATE(datetime)
       ) last ON w.id = last.id

will get you what you want. 会得到你想要的东西。 Why? 为什么? The inner query returns the largest (meaning most recent) id value for each date in weatherdata . 内部查询返回weatherdata每个日期的最大(意味着最近的) id值。 This can be very fast indeed, especially if you put an index on the datetime column. 这确实可以非常快,尤其是如果您在datetime列上放置了索引。

But it's possible the conditions for this to work don't hold. 但是可能无法满足此条件。 If your datetime column sometimes gets updated to change the date, it's possible that larger id values don't always imply larger datetime values. 如果您的datetime列有时会更新以更改日期,则较大的id值可能并不总是意味着较大的datetime值。

In that case you need something like this. 在这种情况下,您需要像这样的东西。

SELECT w.*
  FROM weatherdata w
  JOIN (   SELECT MAX(datetime) datetime
             FROM weatherdata
            GROUP BY DATE(datetime)
       ) last ON w.datetime = last.datetime

Your query doesn't work because it misuses the nasty nonstandard extension to MySQL GROUP BY. 您的查询无效,因为它滥用了对MySQL GROUP BY的讨厌的非标准扩展。 Read this: https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html 阅读此内容: https : //dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

It should, properly, use the ANY_VALUE() function to highlight the unpredictability of the results. 它应该正确地使用ANY_VALUE()函数突出显示结果的不可预测性。 It shoud read .... 应该阅读....

SELECT MAX(datetime) Stamp, ANY_VALUE(temp) temp

which means you aren't guaranteed the right row's temp value. 这意味着您不能保证右行的临时值。 Rather, it can return the temp value from any row in each day's grouping. 相反,它可以从每天分组的任何行中返回临时值。

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

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