Deleting Records

后端 未结 3 1827
清酒与你
清酒与你 2021-01-25 13:13

I have a table [user_logs] with the following fields [username], [datetimelog]

Sample Data

==============
user1   2011-06-28 08:49:01
user2   2011-06-28          


        
相关标签:
3条回答
  • 2021-01-25 13:24
    DELETE FROM table a WHERE time != (SELECT MAX(time) FROM table b WHERE b.user=a.user);
    

    Here delete a row, if its not the maximum time in group with the same user_id

    0 讨论(0)
  • 2021-01-25 13:34
    DELETE from user_logs UL1, user_logs UL2
    where UL1.username =UL2.datetimelog
    and UL1.datetimelog < UL2.datetimelog
    

    Try that

    0 讨论(0)
  • 2021-01-25 13:50

    What about:

    DELETE FROM 
        MY_TABLE M -- delete from the table
    LEFT JOIN
        MY_TABLE M2 ON M.user = M2.user -- table needs to be joined TO ITSELF
    WHERE
        NOT M.LOG_DATE = MAX( M2.LOG_DATE ) -- Anything which isn't MAX goes.
    

    Could that work?

    0 讨论(0)
提交回复
热议问题