简体   繁体   中英

Last login details of a user

This is the query I am using

ResultSet rsta3=st.executeQuery("Select * FROM login_details WHERE name ='"+name1+"' AND intime = (select max(intime) from login_details where name = '"+name1+"')");

With this I get the current login details.How to get the previous login details?What query should I use?

在此处输入图片说明

The query given in the below answers gives only 11.44 time

你找到这个

Select * FROM login_details WHERE name ='"+name1+"' order by intime desc LIMIT 1,1

How about

Select * FROM login_details WHERE name ='"+name1+"' order by intime desc LIMIT 1,1

this should retrieve the second oldest record

Also as mentioned due to subsequent findings, as your DB is incorrectly storing your Date data as Varchar, your can use the below query until your fix your DB.

Select * FROM login_details WHERE name ='"+name1+"' order by id desc LIMIT 1,1
ResultSet rsta3=st.executeQuery("Select * FROM login_details WHERE name ='"+name1+"' AND intime = (select max(intime) from login_details where name = '"+name1+"' order by intime desc limit 1,1)");

尝试这个

Your Query is right, but you are fetching the data after inserting the current login session.

So, once you validate your for user name and password, get the last login details using your query and then update your table with new login time.

Even the fetching with ORDER BY LIMIT should work.

Try something like this :

Select * from login_details where name = '"+name+"' order by outtime desc limit 1,1.

The sole idea is firstly sort the list in descending order by time which in your case is : (outtime) will display the result in the order you want. Then using limit fetch only the first record .

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