处理返回多行的mySQL子查询

时间:2011-04-27 17:55:09

标签: mysql

以下是我的例子:

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

我遇到的问题是,如果子查询返回多行,我的查询需要返回多行。

理想情况下,它会转化为类似的内容:

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

我怎样才能实现这一目标?谢谢!

2 个答案:

答案 0 :(得分:15)

您正在寻找IN子句

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

答案 1 :(得分:2)

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