WildBill
WildBill

Reputation: 83

How to use selected value from table and use it for next selection from another table in one query?

I have two tables,for example:

table_1 (id_t1,id_t2,...);

table_2 (id_t2,...);

I know id_t1 of the table_1 and I need to get all rows from table_1 by the value of id_t2 with a join table 2. Can I do it for one query at MariaDB?

Now I can do this:

select id_t2 from table_1;

select * 
from table_1 left join table_2 on table_1.id_t2=table_2.id_t2 
where id_t2=the result of past query;

Upvotes: 0

Views: 397

Answers (1)

Erubiel
Erubiel

Reputation: 2972

Try with a IN statement

SELECT * 
FROM table_1 LEFT JOIN table_2 ON table_1.id_t2=table_2.id_t2 
WHERE table1.id_t2 IN (select tAux.id_t2 from table_1 as tAux);

Upvotes: 1

Related Questions