简体   繁体   English

mySQL 获取具有列最大值的行

[英]mySQL Fetch the row which has the Max value for a column

How can I fetch the full row that contain the max(column).如何获取包含 max(column) 的整行。
Example:例子:
I have a table like this我有一张这样的桌子

+----+---------------+------+---------------------+
| id | title         | c    | update              |
+----+---------------+------+---------------------+
|  1 | John S.       |   15 | 2017-09-15 09:04:13 |
|  1 | John S.       |   21 | 2017-09-15 09:04:29 |
|  1 | John S.       |   22 | 2017-09-16 01:55:26 |
| 43 | Cristover Co. |   15 | 2017-09-17 14:11:43 |
| 43 | PeterSberg R. |   16 | 2017-09-17 15:12:30 |
| 43 | Cristover Co. |   19 | 2017-09-17 21:45:10 |
+----+---------------+------+---------------------+

Now I want to select the row that contains the max(update).现在我想选择包含 max(update) 的行。 Expected result:预期结果:

+----+---------------+------+---------------------+
| id | title         | c    | update              |
+----+---------------+------+---------------------+
| 43 | Cristover Co. |   19 | 2017-09-17 21:45:10 |
+----+---------------+------+---------------------+

try this one试试这个

SELECT * FROM table_name 
    WHERE table_name.update = 
    (SELECT max(table_name.update) from table_name)

First, you shouldn't name your column update , choose another name, let's say "date_of_update"首先,你不应该命名你的列update ,选择另一个名称,让我们说“date_of_update”

If you want to select only the row with the most recent update, you can simply use something like this :如果您只想选择具有最新更新的行,您可以简单地使用以下内容:

SELECT * FROM table_name ORDER BY date_of_update DESC LIMIT 1.

This will give you only one row with the max date!这只会给你一行最大日期!

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

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