Mysql:获取最受欢迎的列值并返回每个值的最新行

Mysql:获取最受欢迎的列值并返回每个值的最新行

我需要获取最常见的 10 个列值,然后从这些值中取出最新的行。

我可以很容易地获取最近的 10 个列值,如下所示:

SELECT animal, count(*) FROM mammals GROUP BY animal ORDER BY count(*) DESC LIMIT 10;

我还可以像这样获取最新行的值:

SELECT * FROM mammals where animal = 'monkey' ORDER BY check_date DESC LIMIT 1;

如果可能的话,我需要在一个查询中同时执行这两项操作。我得到的最接近的查询满足了我的所有要求,但它没有获取值的最新行,而是获取了第一行。

SELECT animal, check_date, count(*) FROM mammals GROUP BY animal ORDER BY count(*) DESC LIMIT 10;

如果我有这样的表格:

+------------------------+---------------------+
| Monkey                 | 2017-05-08 19:35:30 |
| Monkey                 | 2017-05-09 08:33:16 |
| Giraffe                | 2017-05-09 08:35:24 |
| Giraffe                | 2017-05-09 09:09:25 |
| Monkey                 | 2017-05-09 09:22:43 |
| Giraffe                | 2017-05-09 09:24:23 |
| Giraffe                | 2017-05-09 09:25:07 |
| Monkey                 | 2017-05-09 09:26:00 |
| Lion                   | 2017-05-09 09:26:17 |
| Lion                   | 2017-05-09 09:28:35 |
| Gazelle                | 2017-05-09 09:29:34 |
| Monkey                 | 2017-05-09 13:29:39 |
| Gazelle                | 2017-05-09 13:35:01 |
| Gazelle                | 2017-05-09 14:52:57 |
+------------------------+---------------------+

我运行上面的第三个查询,结果如下:

+------------------+---------------+----------------+
| Animal           | check_date          | count(*) |
+------------------+---------------+----------------+
| Monkey           | 2017-05-08 19:35:30 |        5 |
| Giraffe          | 2017-05-09 08:35:24 |        4 |
| Gazzelle         | 2017-05-09 09:29:34 |        3 |
| Lion             | 2017-05-09 09:26:17 |        2 |
+------------------+---------------+----------------+

表格显然会比这大得多,但想象一下这些是最常见的值。如您所见,第三个查询返回的行是最旧的行,但我需要最新的行。因此对于 Monkey 来说,它将是“2017-05-09 13:29:39”。

我需要表格看起来像这样:

+------------------+---------------+----------------+
| Animal           | check_date          | count(*) |
+------------------+---------------+----------------+
| Monkey           | 2017-05-09 13:29:39 |        5 |
| Giraffe          | 2017-05-09 09:25:07 |        4 |
| Gazzelle         | 2017-05-09 14:52:57 |        3 |
| Lion             | 2017-05-09 09:28:35 |        2 |
+------------------+---------------+----------------+

谢谢 :)

答案1

事实上,这很容易。

附下表:

mysql> select * from mammals;
+----+---------+---------------------+
| id | animal  | check_date          |
+----+---------+---------------------+
|  1 | Monkey  | 2017-05-08 19:35:30 |
|  2 | Monkey  | 2017-05-09 08:33:16 |
|  3 | Monkey  | 2017-05-09 09:22:43 |
|  4 | Monkey  | 2017-05-09 09:26:00 |
|  5 | Monkey  | 2017-05-09 13:29:39 |
|  6 | Giraffe | 2017-05-09 08:35:24 |
|  7 | Giraffe | 2017-05-09 09:09:25 |
|  8 | Giraffe | 2017-05-09 09:24:23 |
|  9 | Giraffe | 2017-05-09 09:25:07 |
| 10 | Lion    | 2017-05-09 09:26:17 |
| 11 | Lion    | 2017-05-09 09:28:35 |
| 12 | Gazelle | 2017-05-09 09:29:34 |
| 13 | Gazelle | 2017-05-09 13:35:01 |
| 14 | Gazelle | 2017-05-09 14:52:57 |
+----+---------+---------------------+
14 rows in set (0.00 sec)

查询:

选择动物,max(check_date)作为“check_date”,count() 来自哺乳动物组按 1 排序按 3 降序排列;

给出你正在搜索的内容:

mysql> select animal,max(check_date) as "check_date",count(*) from mammals group by 1 order by 3 desc;
+---------+---------------------+----------+
| animal  | check_date          | count(*) |
+---------+---------------------+----------+
| Monkey  | 2017-05-09 13:29:39 |        5 |
| Giraffe | 2017-05-09 09:25:07 |        4 |
| Gazelle | 2017-05-09 14:52:57 |        3 |
| Lion    | 2017-05-09 09:28:35 |        2 |
+---------+---------------------+----------+
4 rows in set (0.00 sec)

PS:你的问题并不完全是“主题”,在这里,服务器故障。也许你应该考虑在dba.stackexchenge

相关内容