Kamil Owczarek
Kamil Owczarek

Reputation: 59

SQL query to get 2 values from table 1 and join all possible option from table 2

I want to get value from table1 and join all matching value from table2. The table1 has to be limited to 2 rows, but expecting output should own all matching values for those two ids.

enter image description here

How can I achieve this?

Upvotes: 0

Views: 805

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133390

if you want all the value in join for only two row of table 1 you can use a subqiuery with limit 2

select b.id, a.value, b.value2, b.table1_ID
from (
    select * from table1
    limit 2

) a 
inner join table2 on aid = b.table1_ID

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270713

You would use a subquery:

select t1.*, t2.*
from (select t1.*
      from table1 t1
      limit 10
     ) t1 left join
     table2 t2
     on t1.id = t2.table1_id;

Note: This returns two arbitrary rows. Normally, you would have an order by to better specify the rows. And use order by rand() for random rows.

Upvotes: 1

Related Questions