Solved

Need Help with a SQL query to select records with last action < 5 mins ago

Posted on 2014-08-13
3
112 Views
Last Modified: 2015-02-06
I have a users table with a field "last_action" (DATETIME field) which records the time of the last activity of a user on the site. This allows us to show the users as "online" or not.

Please tell me the mysql query needed to pull all users who's "last_action" field is 5 minutes ago or less. .... something like:

select * from users where last_action <= 10minutesago

Thanks!
0
Comment
Question by:tonyhhisc
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
rjohnsonjr earned 500 total points
ID: 40259799
Try this:

select * from users where last_action <= DATE_SUB(NOW(), INTERVAL 10 MINUTE)

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40259840
Five minutes or ten minutes?  With computers, accuracy matters!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40594948
Yep, it's either five or ten and after waiting for months to hear back from you, we still don't know what you really want!  

But that aside, you cannot really know whether a user is "online" or not.  What if I visit your site and then turn off my computer, or leave and get some coffee?  Am I "online?"  The nature of the HTTP request/response system can only tell you if your server has gotten a request, not whether a client is paying attention to the response.  You can use AngularJS or jQuery to repeat the requests over and over, thus getting a string of client requests, and you can make some assumptions about whether the user is "online" based on the timing of the string of repeated requests.  But even these assumptions are uncertain -- I can open a browser window and visit your site, thus starting the jQuery timers that would repeatedly query your server.  Then the question becomes again, Am I "online?"  Or have I just left the browser tab open while I went out coffee?

It seems that your strategy will tell you if the client visited the site in some recent period, but that's about all you can know - the whole concept of an "online user" is seductive, but does not exist in the RESTful world of the WWW.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikipedia.org/wiki/PHP  Very powerful.  But a…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question