MySQL查询在表中查找最新的3个日期 [英] MySQL query to find latest 3 dates in a table

查看:92
本文介绍了MySQL查询在表中查找最新的3个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表实际上是这样的:

My table actually looks like this:

date           value
2015-06-27     8088.15
2015-06-27     8083.25
2015-06-27     8068.25
2015-06-24     8246.45
2015-06-24     8343.55
2015-06-24     8232.35
2015-06-24     8564.65
2015-06-23     8354.75
2015-06-23     8455.25
2015-06-23     8451.45
2015-06-22     8343.55
2015-06-22     8345.75
2015-06-21     8023.75
2015-06-21     8029.85
2015-06-20     8023.95

我想从这样的表格中查询最新的3个日期。我期望的输出是:

I want to query latest 3 dates from a table like this. The output I am expecting is:

2015-06-27
2015-06-24
2015-06-23

目的是找出最大值(最小值)和最小值)最近3天。表中的日期是星期几的日期,但可以是假日,每天可以有无限的行数。

The purpose of this is to find out the max(value) and min(value) for last 3 days. The dates in the table are dates of week days but there can be holidays and every day can have indefinite number of rows.

如何从表格中查询3个最新日期?

我尝试过这个从table_name中选择日期按日期desc 。这不是我正在寻找的正确输出。

How can I query 3 latest dates from table?
I tried this select date from table_name order by date desc. This is not giving the right output I am looking for.

推荐答案

获取唯一的日期,然后使用 限制

Get the unique dates and then use order by and limit:

select date
from (select distinct date from table t) d
order by date desc
limit 3;

这篇关于MySQL查询在表中查找最新的3个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆