There are two tables

  1. message: id, from (iduser), to (iduser), message

  2. user id

How to make a request from message so that as a result instead of from (iduser) was the name from the user table?

  • about JOIN did not hear or did not master? - Fat-Zer
  • and how JOIN solves the problem? SELECT * FROM message LEFT JOIN user ON user.id = message.from or user.id = message.to gives 2 lines with names. And I need both values ​​to be on the same line. - Typography Manager
  • it means that I didn’t master it ... yes, and there is no requirement in the question to dereference the second key ... - Fat-Zer
  • In principle, let the keys remain. It is necessary that the result be something like this: id, from (iduser), fromname (username by id), to (iduser), toname (username by id) - Typography Manager
  • Already gave the answer ... - Fat-Zer

1 answer 1

To dereference two foreign keys you need two JOIN 'a.

 SELECT u_from.name, u_to.name, message.message FROM message LEFT JOIN user AS u_from ON message.from_id = u_from.id LEFT JOIN user AS u_to ON message.to_id = u_to.id; 
  • you need two JOIN 'a ... and respectively two copies of the user table. - Akina