M
M
Miku Hatsune2016-03-15 14:15:16
PHP
Miku Hatsune, 2016-03-15 14:15:16

Sorting elements from one table based on data in another?

There are 2 tables, one is rooms (rooms), the second is users in the room (usersRoom)
I need to display the rooms based on how many users are in the room in descending order. How to do it?
At least I'll give you an example, I'll figure it out there ;)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Eremin, 2016-03-15
@Hatsune-Miku

I would go from this: I don’t have the opportunity to check anywhere now, but it should work:
The idea is this:

SELECT RMS.ID FROM rooms RMS
ORDER BY (SELECT COUNT(Id) FROM usersRoom WHERE roomId = RMS.ID ) DESC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question