O
O
Oleg Otkidach2020-11-15 18:40:43
SQL
Oleg Otkidach, 2020-11-15 18:40:43

From two pairs of values ​​found by a certain query, how to get the values ​​associated with them from neighboring tables?

Suppose I made some ingenious SELECT (inside it there are still selects and all sorts of WHERE, GROUP, HAVING), which resulted in me, say, two lines in two columns.
All four resulting values ​​are id-shki.
In the end, instead of these id-niks, I need to get their names, which are stored in two more neighboring tables.

I can get the first column, but there are problems with the second one.
So to speak:

SELECT table1.name1, table2.name2
FROM table1, table2
WHERE table1.id IN 
        (column_1 IN 
                (мой хитроумный первый SELECT)
        )


And then what?
If I write

AND table2.id IN
        (column_2 IN 
                (мой хитроумный первый SELECT)
        )


then they will give me 4 lines, not the required two.

If I write

AND table2.id =
        (column_2 IN 
                (мой хитроумный первый SELECT)
        )


I will be told that my dodgy returns more than one record.

How should this be resolved?
I hope I phrased everything correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman, 2020-11-15
@Allegro75

SELECT 
 t1.name, t2.name
FROM (<your_monstrum_query>) alias
JOIN table1 t1 ON t1.id = alias.id1
JOIN table2 t2 ON t2.id = alias.id2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question