如何按MAX(日期)选择?

时间:2011-10-20 12:39:09

标签: mysql sql select max

这是表结构

CREATE TABLE `reports` (
  `report_id` int(11) NOT NULL auto_increment,
  `computer_id` int(11) NOT NULL default '0',
  `date_entered` datetime NOT NULL default '1970-01-01 00:00:00',
  `total_seconds` int(11) NOT NULL default '0',
  `iphone_id` int(11) default '0',
  PRIMARY KEY  (`report_id`),
  KEY `computer_id` (`computer_id`),
  KEY `iphone_id` (`iphone_id`)
) ENGINE=MyISAM AUTO_INCREMENT=120990 DEFAULT CHARSET=latin1

我需要一个SELECT语句,该语句会从最新输入的report_id中列出computer_iddate_entered个{{1}},我不知道如何做到这一点。有人能指出我正确的方向吗? Thx提前。

12 个答案:

答案 0 :(得分:42)

这应该这样做:

SELECT report_id, computer_id, date_entered
FROM reports AS a
WHERE date_entered = (
    SELECT MAX(date_entered)
    FROM reports AS b
    WHERE a.report_id = b.report_id
      AND a.computer_id = b.computer_id
)

答案 1 :(得分:13)

您是否只希望它显示最后一个date_entered,或者从输入的last_date开始订购?

SELECT report_id, computer_id, date_entered
FROM reports
GROUP BY computer_id
ORDER BY date_entered DESC
-- LIMIT 1 -- uncomment to only show the last date.

答案 2 :(得分:3)

根据这一点:https://bugs.mysql.com/bug.php?id=54784施放为char应该可以解决问题:

SELECT report_id, computer_id, MAX(CAST(date_entered AS CHAR))
FROM reports
GROUP BY report_id, computer_id

答案 3 :(得分:0)

这是一个非常老的问题,但是由于同样的问题,我来到这里,所以我将其留在这里以帮助其他人。

我正在尝试优化查询,因为由于数据量大,查询数据库需要5分钟以上。我的查询类似于接受的答案的查询。 Pablo's comment将我推向正确的方向,我的5分钟查询时间变成了0.016秒。因此,为帮助查询时间较长的其他用户,请尝试使用uncorrelated subquery

OP的示例为:

SELECT 
    a.report_id, 
    a.computer_id, 
    a.date_entered
FROM reports AS a
    JOIN (
        SELECT report_id, computer_id, MAX(date_entered) as max_date_entered
        FROM reports
        GROUP BY report_id, computer_id
    ) as b
WHERE a.report_id = b.report_id
    AND a.computer_id = b.computer_id
    AND a.date_entered = b.max_date_entered

感谢Pablo的评论。你节省了我很多时间!

答案 4 :(得分:0)

解决方法但可行的解决方案

仅当ID为自动递增时,您才可以搜索最大ID,而不是最大日期。 因此,通过ID,您可以找到所有其他字段。

select *
from table
where id IN ( 
              select max(id)
              from table
              group by #MY_FIELD#
              )

答案 5 :(得分:0)

我使用此解决方案,效果很好

选择输入的报告ID,计算机ID,日期 来自报告 GROUP BY computer_id 具有最大值(已输入日期)

答案 6 :(得分:0)

对我来说很完美:

(SELECT content FROM tblopportunitycomments WHERE opportunityid = 1 ORDER BY dateadded DESC LIMIT 1);

答案 7 :(得分:0)

对我来说很好

SELECT report_id,computer_id,MAX(date_entered) FROM reports GROUP BY computer_id

答案 8 :(得分:0)

如果您使用的是当前时间戳,这将非常完美

SELECT * FROM reports WHERE date_entered = (SELECT max(date_entered) FROM REPORTS)

如果您未使用当前时间戳,但您分别使用了日期和时间列,这也将起作用

SELECT * FROM reports WHERE date_entered = (SELECT max(date_entered) FROM REPORTS) ORDER BY time DESC LIMIT 1

答案 9 :(得分:0)

select report_id, computer_id, date_entered
into #latest_date
from reports a
where exists(select 'x' from reports 
                where a.report_id = report_id
                group by report_id having max(date_entered) =   a.date_entered)

select * from #latest_leave where computer_id = ##

答案 10 :(得分:-1)

str.matches(".*[^$_#?\\d\\p{L}].*")

答案 11 :(得分:-1)

在博客引擎上进行此操作以获取最新的博客。我将其调整为您的表结构。

SELECT * FROM reports WHERE date_entered = (SELECT max(date_entered) FROM REPORTS)