m r
m r

Reputation: 2937

Handling of mySQL sub-query returning multiple rows

Here is my example:

select row_x from table_1 where row_y = (select row_a from table_2 where row_b = x)

The problem that I am running into is that my query needs to return multiple rows if the subquery returns multiple rows.

Ideally it would translate to something similar to:

 'select row_x from table_1 where row_y = '<first row from subquery>' or row_y = '<second row from subquery>' etc.

How can I make this happen? Thanks!

Upvotes: 7

Views: 18358

Answers (2)

piotrm
piotrm

Reputation: 12356

SELECT t1.row_x FROM table_1 t1 JOIN table_2 t2 ON t1.row_y=t2.row_a WHERE t2.row_b = x

Upvotes: 3

Shakti Singh
Shakti Singh

Reputation: 86336

You are looking for IN clause

select row_x from table_1 
 where row_y 
 IN (
     select row_a from table_2 where row_b = x
    )

Upvotes: 16

Related Questions