I'm working on a networking/social website and I'm trying to create a function to return the users that are visiting the same page at the moment you visit it.
How can one select the users from the table "USERS" by counting and grouping the user_id from the table "READING"?
Database structure is the following :
USERS
- user_id
- username
- ...
READING
- id
- user_id
- update_timestamp
- ...
What should be returned is a array like this
user
- user_id
- user_name
- user_first_name
- ...
user
- user_id
- ...
This is the query that I have so far
SELECT r.user_id, u.* FROM reading r, users u GROUP BY r.user_id AS count WHERE u.user_id = r.user_id ORDER BY count DESC
Example tables
USERS
// user_id // user_name // user_email
-------------------------------------
123 / mentos / [email protected]
-------------------------------------
321 / freshmaker / [email protected]
-------------------------------------
231 / hubba / [email protected]
READING
// id // user_id // timestamp
--------------------------------------
1 / 123 / 201501050420
2 / 321 / 201501050420
Example of what the output should be
-ARRAY
-user
- 123
- mentos
- [email protected]
-user
- 321
- freshmaker
- [email protected]
COUNT()
. What do you mean by that? And how do you determine the "at the same time" criterion? You havereading.update_timestamp
-- should there be some threshold, sayupdate_timestamp
has a value within the past 60 seconds? – Michael Berkowski