如何选择MAX(日期)?

时间:2023-01-13 22:47:23

This is the table structure

这是表格结构。

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

I need a SELECT statement that will list the report_id per computer_id from latest entered date_entered, and i have no clue how to do that. Can anyone point me into the right direction? Thx in advance.

我需要一个SELECT语句,它将列出来自最新输入的date_enter的每个computer_id,我不知道如何做到这一点。谁能给我指出正确的方向吗?提前谢谢。

4 个解决方案

#1


34  

This should do it:

这应该这样做:

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
)

#2


11  

Are you only wanting it to show the last date_entered, or to order by starting with the last_date entered?

您只希望它显示最后一个date_enter,还是从输入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.

#3


4  

Accordig to this: https://bugs.mysql.com/bug.php?id=54784 casting as char should do the trick:

根据这个: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

#4


0  

SELECT report_id, computer_id, date_entered
FROM reports
WHERE date_entered = (
    SELECT date_entered 
    FROM reports 
    ORDER date_entered 
    DESC LIMIT 1
)

#1


34  

This should do it:

这应该这样做:

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
)

#2


11  

Are you only wanting it to show the last date_entered, or to order by starting with the last_date entered?

您只希望它显示最后一个date_enter,还是从输入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.

#3


4  

Accordig to this: https://bugs.mysql.com/bug.php?id=54784 casting as char should do the trick:

根据这个: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

#4


0  

SELECT report_id, computer_id, date_entered
FROM reports
WHERE date_entered = (
    SELECT date_entered 
    FROM reports 
    ORDER date_entered 
    DESC LIMIT 1
)