1

My database holds a 'lastOnline' timestamp variable that get updates when a user logs in. I want to use this to retrive 3 values

[The total amount of users][users online today][users online in the last hour].

I can do each statement seperately

SELECT count(u.id) AS usersTotal FROM user u

SELECT count(u.id) AS usersDay FROM user u
WHERE u.lastOnline >= now() - INTERVAL 1 DAY

SELECT count(u.id) AS usersHour FROM user u
WHERE u.lastOnline >= now() - INTERVAL 1 HOUR

but I am having trouble joining/union the 3 of them into one query

Thank you

1 Answer 1

1

You could use:

SELECT count(u.id) AS usersTotal 
      ,SUM(u.lastOnline >= now() - INTERVAL 1 DAY)  AS usersDay
      ,SUM(u.lastOnline >= now() - INTERVAL 1 HOUR) AS usersHour
FROM user u;

How it works:

u.lastOnline >= now() - INTERVAL 1 DAY return true(1) or false(0).

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

1 Comment

Perfect... Thanks alot!

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.