簡體   English   中英

如何按 MAX(日期)選擇?

[英]How to SELECT by MAX(date)?

這是表結構:

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_idcomputer_id從最近輸入的date_entered ,我不知道該怎么做。

這應該這樣做:

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
)

您是只想顯示最后輸入的日期,還是從輸入的最后一個日期開始排序?

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.

根據這一點: https ://bugs.mysql.com/bug.php?id = 54784 cast as char 應該可以解決問題:

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

這是一個非常古老的問題,但我是因為同樣的問題來到這里的,所以我把它留在這里是為了幫助其他人。

我試圖優化查詢,因為由於數據量,查詢數據庫需要 5 多分鍾。 我的查詢類似於接受的答案的查詢。 Pablo 的評論將我推向了正確的方向,我的 5 分鍾查詢變成了 0.016 秒。 因此,為了幫助任何其他查詢時間很長的人,請嘗試使用不相關的子查詢

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

謝謝巴勃羅的評論。 你救了我很多時間!

這對我很有效

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

解決方法但有效的解決方案

僅當 ID 為 autoincrement 時,您可以搜索最大 id 而不是最大日期。 因此,通過 ID,您可以找到所有其他字段。

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

非常適合我:

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

如果您使用當前時間戳,這將完美地工作

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
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 = ##
SELECT report_id, computer_id, date_entered
FROM reports
WHERE date_entered = (
    SELECT date_entered 
    FROM reports 
    ORDER date_entered 
    DESC LIMIT 1
)

我使用這個having max(date_entered)解決方案並且效果很好

SELECT 
  report_id, 
  computer_id, 
  date_entered
FROM reports
GROUP BY computer_id having max(date_entered)

在博客引擎上執行此操作以獲取最新博客。 我根據你的表結構調整了它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM