0

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

+----------+------------+---------------------+
|    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值进行另一次搜索,但我不知道如何在一个命令中完成这一切,或者即使有办法。

4

2 回答 2

2

使用子查询

SELECT * from 'MyTable' WHERE 'date_updated'=(
    SELECT date_updated FROM 'MyTable' ORDER BY date_updated DESC LIMIT 1)
于 2013-02-10T17:54:07.237 回答
2
SELECT * FROM MyTable WHERE date_updated = (SELECT MAX(date_updated) FROM MyTable)
于 2013-02-10T17:55:15.890 回答