简体   繁体   中英

How to Select these record by MYSQL Statement?

I have a Table:

tblUserLoginRecord  
ID ||  User || LoginTime  
1  ||  Peter|| 2017-07-03  
2  ||  Susan|| 2017-07-04  
3  ||  Sam  || 2017-07-05  
4  ||  Sam  || 2017-07-07  
5  ||  Sam  || 2017-07-08  
6  ||  Susan|| 2017-07-09    

I want to have a result that show the last LoginTime Group By User(Using ID DESC will be fine too):

ID ||  User || LoginTime  
1  ||  Peter|| 2017-07-03  
5  ||  Sam  || 2017-07-08  
6  ||  Susan|| 2017-07-09 

How I do it now is:

SELECT * FROM (SELECT * FROM tblUserLoginRecord ORDER BY ID DESC) a GROUP BY User;  

But it will be much slower if the table growth bigger. How can I make it better? Thank you.

You can use select only once if you are using mysql. That will speed up things up.Also your query should be better. See below and use it, you will see the difference

Select ID , User, max(LoginTime) as date
from tblUserLoginRecord  
group by User

You need to build better queries for better results which will help beyond your expectations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM