S
S
sk19872018-03-29 08:30:45
MySQL
sk1987, 2018-03-29 08:30:45

How to merge into one request?

Hello, tell me who knows.
There is a query roughly speaking: SELECT `field1, `field2`, `field3` FROM `table1` WHERE `field4` = '10';
And after this query comes the fetch:
SELECT `login` FROM `users` WHERE `id` = `field2`;
SELECT `login` FROM `users` WHERE `id` = `field3`;
Is it possible to combine everything in one query?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2018-03-29
@sk1987

SELECT
  table1.поле1,
  table1.поле2,
  table1.поле3,
  user1.login AS login1,
  user2.login AS login
FROM `таблица1` AS table1
  LEFT JOIN `users` AS user1 ON user1.id = table1.поле2
  LEFT JOIN `users` AS user2 ON user2.id = table1.поле3
WHERE table1.поле4 = '10';

D
Dmitry Entelis, 2018-03-29
@DmitriyEntelis

SELECT поле1, поле2, поле3, users_2.title as user_2_title, users_3.title as user_3_title
FROM таблица1
left join users as users_2 on  users_2.id = таблица1.поле2
left join users as users_3 on  users_3.id = таблица1.поле2
WHERE поле4 = '10';

L
Lander, 2018-03-29
@usdglander

SELECT `login` FROM `users` WHERE `id` = `поле2`
UNION
SELECT `login` FROM `users` WHERE `id` = `поле3`;

Although you can just
SELECT `login` FROM `users` WHERE `id` = `поле2` OR `id` = `поле3`;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question