MySQL: Add a condition on a subquery from a joint table

mr5

Get all the rooms which this "user" is part of and list each room details including its

  • id (roomId)
  • name (roomName)
  • total active users of this room (activeUsers)
  • total users included in the room (totalUsers)

Provided with the table structure below:

Table:users
+---------+-------------------------+
| id(int) |  active(bool/tiny int)  |
+---------+-------------------------+

Table:rooms
+---------+----------------------+
| id(int) |  name(varchar(255))  |
+---------+----------------------+

Table:room_members
+-------------+---------------+
| userId(int) |  roomId(int)  |
+-------------+---------------+

I'm kind of lost with my SQL so far:

SELECT
    rooms.id,
    rooms.name,
    (SELECT SUM(users.active) FROM users WHERE room_members.roomId = rooms.id) activeUsers,
    (SELECT COUNT(users.id) FROM users WHERE room_members.roomId = rooms.id) totalUsers
FROM room_members
INNER JOIN rooms
ON room_members.roomId = rooms.id
INNER JOIN users
ON room_members.userId = users.id
WHERE room_members.userId = 1

Basing from this data:

sample users data

I can conclude that I've got the wrong results.

capture of mysql query

I guess I need some help from you guys.

scaisEdge

You could use a dynamic table with group by ( and a join between user and room_members)

SELECT
  rooms.id,
  rooms.name,
  t1.activeUsers,
  t1.totalUsers
FROM room_members 
INNER JOIN rooms  ON room_members.roomId = rooms.id
INNER JOIN users  ON room_members.userId = users.id
INNER JOIN  (  
                SELECT roomId, SUM(users.active)  as activeUsers, COUNT(users.id) as totalUsers
                FROM users 
                INNER JOIN room_members on users.id = room_members.userId
                group by roomId
            ) t1 on t1.roomID = rooms.id
WHERE room_members.userId = 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Subquery containing containstable with a condition from querying table

From Dev

Add a mysql subquery to select data from yesterday

From Dev

MySQL Subquery with Averages From Another Table

From Dev

Subquery into a LIKE condition mysql

From Dev

MYSQL and PHP loop and grab just one item from a joint table?

From Dev

MySQL update a table and select from the same table in a subquery

From Dev

Update table based on subquery with if condition

From Dev

Update table in MySQL with subquery

From Dev

MySQL single table subquery

From Dev

add column to table and fill it under condition -mySQL

From Dev

(SQLITE) How to add together sums from another table using a subquery

From Dev

MySQL subquery fetch only latest value from JOIN table

From Dev

Subquery from the same table

From Dev

subquery as the from for mysql

From Dev

selecting duplicate values with a condition from a mysql table

From Dev

mysql LEFT JOIN with condition from other table

From Dev

MySQL Where condition groupings from remote table

From Dev

MySQL Query add limit in SubQuery

From Dev

Getting overall status on joint table in MYSQL

From Dev

MySQL Update with same table subquery

From Dev

Perform a subquery on a spatial table in MySQL

From Dev

MySQL - Referencing an aliased table in a subquery

From Dev

MySQL subquery and temporary table is slow

From Dev

Using subquery for the same table in MySQL

From Dev

MySQL Update with same table subquery

From Dev

MySQL subquery and temporary table is slow

From Dev

subquery mysql on Update in same table

From Dev

MySQL count subquery on same table

From Dev

Mysql multiple condition one column subquery

Related Related

HotTag

Archive