Select everything from the ORDER BY table with another table with a primary matching key

I searched for a while, but the query I'm trying to fulfill is pretty hard to find any information or documentation on how to do what I'm trying to do.

I have two tables, one of which stores my user accounts and basic information. Then I have a second table that contains a bit more information about the user.

Both of these tables have primary keys (the first table idand the second table user_id), which I use to know who is who and to match records between the two tables.

What I'm trying to do today is get 10 entries from table 1, sort by column in table 2 (room_count) DESC.

The name of table No. 1 is "users", and the name of table No. 2 is "user_information".

What have i tried?
I'm not quite sure where to start, so I haven't tried anything yet.

How could I do something like this? Thanks for any posted answers.

For example, let's say I have 4 users, I will write the username followed by the room_count column in the following table below.

Adam Sandler : 4
Jenny Hang : 9
Peter Foreign : 0

If I used a query with ASC, it starts with Peter Foreignand ends withJenny Hang

+4
source share
3 answers

You just do not need a simple connection?

SELECT 
FROM users 
INNER JOIN user_information ON users.id = user_information.user_id 
ORDER BY user_information.room_count DESC
LIMIT 2
+2
source

Please try the following:

SELECT * 
  FROM users u 
 INNER JOIN user_info ui
    ON u.id = ui.user_id 
 ORDER BY ui.room_count DESC
 LIMIT 10
0
source

- ,

select users.*
from
  users, user_information
where
  users.id =user_information.user_id
order by
  user_information.room_count
  desc
Limit 10

Edit: changed select users.idto select users.*to better approach the question.

0
source

Source: https://habr.com/ru/post/1682849/


All Articles