How to SELECT by MAX(date)?

前端 未结 12 1543
独厮守ぢ
独厮守ぢ 2020-12-03 02:49

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_e         


        
相关标签:
12条回答
  • 2020-12-03 03:33

    I use this solution having max(date_entered) and it works very well

    SELECT 
      report_id, 
      computer_id, 
      date_entered
    FROM reports
    GROUP BY computer_id having max(date_entered)
    
    0 讨论(0)
  • 2020-12-03 03:37
    SELECT report_id, computer_id, date_entered
    FROM reports
    WHERE date_entered = (
        SELECT date_entered 
        FROM reports 
        ORDER date_entered 
        DESC LIMIT 1
    )
    
    0 讨论(0)
  • 2020-12-03 03:38
    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 = ##
    
    0 讨论(0)
  • 2020-12-03 03:41

    It works great for me

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

    0 讨论(0)
  • 2020-12-03 03:47

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

    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.
    
    0 讨论(0)
  • 2020-12-03 03:49

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

    SELECT report_id, computer_id, MAX(CAST(date_entered AS CHAR))
    FROM reports
    GROUP BY report_id, computer_id
    
    0 讨论(0)
提交回复
热议问题