-1

I am trying to fetch the most recent record from my DB. The table looks like the below.

Ticket_id|  date|   comments
EWU-752-84170|  4/10/2015 13:26|bla
HCX-943-86125|  4/10/2015 13:39|    ola
IKW-626-96314|  4/10/2015 13:42|    jkj
EWU-752-84170|  4/10/2015 13:28|    blo
EWU-752-84170|  4/10/2015 13:37|    ala
HCX-943-86125|  4/10/2015 15:11|    kbdkj
EWU-752-84170|  4/10/2015 13:43|    cla

and the output should send the most recent records as below.

Ticket_id|  date|   comments
EWU-752-84170|  4/10/2015 13:43|    cla
HCX-943-86125|  4/10/2015 15:11|    kbdkj
IKW-626-96314|  4/10/2015 13:42|    jkj

4 Answers4

0
ORDER BY date DESC limit 1

That should work fine.

Mike
  • 103
  • 1
  • 2
  • 11
0
SELECT t1.*
FROM
  mytable t1 LEFT JOIN mytable t2
  ON
    t1.Ticket_id=t2.Ticket_id
    AND t1.date<t2.date
WHERE
  t2.date IS NULL
fthiella
  • 48,073
  • 15
  • 90
  • 106
0

This will select all tickets where another ticket with a later date does not exist (therefore it selects only the latest tickets)

select * from mytable t1
where not exists (
    select 1 from mytable t2
    where t2.ticket_id = t1.ticket_id
    and t2.date > t1.date
)
FuzzyTree
  • 32,014
  • 3
  • 54
  • 85
0
    select * from dailyhandover t1
where not exists (
    select * from dailyhandover t2
    where t2.ticket_id = t1.ticket_id
    and t2.date > t1.date
) and customer_name='bata'