mysql - How to get first row from each group from a table in sqlitedatabase? -


i have table messages in sqlite database.this table has 3 columns msg_id,sender_id,msg_body. need 1 latest message each user_id , these messages should ordered in desc order. eg. messages table is

 msg_id | sender_id | msg_body --------+-----------+----------    1    |    18     |"something"    2    |    18     |"something"    3    |    19     |"something"    4    |    19     |"something" 

what want table:-

msg_id | sender_id | msg_body -------+-----------+----------   4    |    19     |"something"   2    |    18     |"something" 

try this:

select t1.* mytable t1 join (    select sender_id, max(msg_id) max_msg_id    mytable    group sender_id ) t2 on t1.sender_id = t2.sender_id , t1.msg_id = t2.max_msg_id 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -