0

I am writing a Client Server Application now when the Client sends a command to the Server the Record in the SQL Database is updated and On Update a Timestamp is set on a field. But now I want to read all the Users in the SQL Database that where online in the last hour with the Timestamp.

So this is how far I am:

SELECT username FROM users WHERE lastonline...

lastonline is the field with the timestamp of the users last update. I now have no Idea how to check if he was online in the last hour.

I thank you for you help.

2 Answers 2

1

Select username FROM user WHERE lastoinline>"date now - 1 hour" AND lastoinline<="date now"

Date dеNow = new Date();
Date веBefore = tdNow; tdBefore.setHours(tdBefore.getHours()-1);
String query = "SELECT username FROM user WHERE lastonline>='"+(dtBefore.getTime/1000)+"' AND lastonline<='"+(dtNow.getTime/1000)+"'

"

Sign up to request clarification or add additional context in comments.

Comments

0

Compare the current hour with the hour on the timestamp's record.

If you are using sql server:

SELECT DATEPART(hh, GETDATE()) --current hour

SELECT DATEOART(hh, ColumnStoredInDB) --stored hour

Spoiler: Simply take the ColumnStoredInDB from the current hour and if the difference is < 1 that user has been in the system for the last hour. If the difference >=1 you can skip that record

5 Comments

Is DATEPART function generic SQL function or is it RDBMS specific?
You didnt tag your question with the RDBMS, I was assuming SQL Server, if not Oracle and other vendors have something similiar. So please consult the documentation based on what you are using...
Sorry for net mentioning I am using MySQL
mysql has an hour function dev.mysql.com/doc/refman/5.0/en/…
I didn't create this question. I was just asking because there is no tag of the specific RDBMS that the OP used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.