【问题标题】:How to fetch the last batch of updates from a MySQL database如何从 MySQL 数据库中获取最后一批更新
【发布时间】:2013-01-25 21:04:31
【问题描述】:

我在名为MyTable 的 MySQL 数据库表中有以下数据:

+----------+------------+---------------------+
|    my_id | country_gb | date_updated        |
+----------+------------+---------------------+
|       10 |        224 | 2013-02-07 00:00:00 |
|       30 |        224 | 2013-02-07 00:00:00 |
|       40 |        299 | 2013-02-07 00:00:00 |
|       60 |        277 | 2013-02-09 00:00:00 |
|       70 |        222 | 2013-02-09 00:00:00 |
|      100 |        349 | 2013-02-09 00:00:00 |

如何查询数据库,只返回日期最新的行?

我知道我可以做SELECT * from 'MyTable' WHERE 'date_updated'="2013-02-09 00:00:00",但如果我现在不做具体的date_updated(因为我没有)怎么办?我想我可以在一个命令中按 date_updated DESC 对表进行排序(类似于SELECT date_updated FROM 'MyTable' ORDER BY date_updated DESC LIMIT 1),然后使用date_updated 值进行另一次搜索,但我不知道如何在一个命令中完成所有这些操作,或者即使有是一种方式。

【问题讨论】:

    标签: mysql date sorting


    【解决方案1】:
    SELECT * FROM MyTable WHERE date_updated = (SELECT MAX(date_updated) FROM MyTable)
    

    【讨论】:

      【解决方案2】:

      使用子查询

      SELECT * from 'MyTable' WHERE 'date_updated'=(
          SELECT date_updated FROM 'MyTable' ORDER BY date_updated DESC LIMIT 1)
      

      【讨论】:

        猜你喜欢
        • 2021-01-02
        • 1970-01-01
        • 2022-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-03
        相关资源
        最近更新 更多