如何从MySQL服务器数据库中获取特定条件的最新记录?

时间:2014-01-22 19:10:23

标签: mysql

我有一个数据库表,其中包含下面简化示例中描述的字段。

获取特定类型值的最新记录的sql查询是什么样的(比如type value = 4,参见示例)?

id - type -   details   - created
 1     4     'detailsA'   2010-09-07
 2     4     'detailsB'   2010-09-10   //this is the record to be retrieved
 3     3     'detailsC'   2010-09-14   

我看到的唯一方法是通过2个查询;第一个查询通过MAX(日期)检索相关日期(2010-09-10),并在第二个查询中使用该值......

2 个答案:

答案 0 :(得分:2)

select id, type, details, created
from MyTable
where type = 4
order by created desc
limit 1

答案 1 :(得分:0)

试试这个: -

select id, type, details, created
from MyTable
where type = 4
order by created desc
limit 0,1;

如果在同一天输入了多条记录,则可以使用以下查询。

 select id, type, details, created
    from MyTable
    where type = 4
    order by id desc
    limit 0,1;