There is a users table in which user data is stored.
Users provide services to each other and leave feedback about each other. There are user_id fields, photo .
In one request, you need to pull users.photo as user1 and users.photo as user2 , knowing the user_id both participants. Is it possible

  • The question allows for different interpretations and frankly incomprehensible. The structure of the fields is clear that you want to receive - no. - AK
  • Maybe these two lines (with user_id 1 and 2) have a field with a common foreign key? By the same principle, you select exactly these two users. - Matty

3 answers 3

Formally, your description includes a stupid query:

 SELECT `ID`, (SELECT photo FROM `b_user` WHERE id = 1) AS photo1, (SELECT photo FROM `b_user` WHERE id = 3) AS photo2 FROM `b_user` WHERE ID IN (1,3) 

Or - on JOINs, if you understand this syntax (not sure how the question is asked):

 SELECT u1.photo AS photo1, u2.photo AS photo2 FROM b_user u1 INNER JOIN b_user u2 WHERE u1.id = 1 AND u2.id = 3; 

But it seems to me that this is not what you want.

I suggest you describe the problem you want to solve your question. See here: problem XY .

  • The stupid query worked!) - Newbie
  • Chesslovo, it would be better if the Akina variant suits you. There is logic there, it is clear what it is being taken for, and the task that you are solving is understandable. And so I feel like a person who helps to nail a microscope with a microscope. Still, think at your leisure what kind of meta-task you decide, you might want to ask so and find out other options besides being “worked”. - AK
  • In fact, the entire request is much more complicated, and we remember the main rule of the programmer - if the code works, do not touch anything!) Forgive me for your feelings) - Newbie

Users provide services to each other and leave feedback about each other.

Most likely, it is fixed in a certain table.

 CREATE TABLE `Услуги_И_Отзывы` (`Кто`, `Кому`, `прочие_поля`) 

Well tady

 SELECT u1.photo as user1, u2.photo as user2, `Всё остальное` FROM `Услуги_И_Отзывы` main , users u1 , users u2 WHERE main.`Кто` = u1.user_id AND main.`Кому` = u2.user_id /* AND всё остальное */ 
  • Yes, I suspect that this is the top-starter version with the table not described in the question - that's why I suggested voicing the original problem that it is trying to solve. - AK
 SELECT u1.photo AS user1, u2.photo AS user2 FROM users u1 CROSS JOIN users u2 WHERE u1.user_id = 1 AND u2.user_id = 2;