处理返回多行的mySQL子查询

时间:2022-10-29 00:08:58

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!

我怎样才能做到这一点?谢谢!

2 个解决方案

#1


15  

You are looking for IN clause

您正在寻找IN条款

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

#2


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

#1


15  

You are looking for IN clause

您正在寻找IN条款

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

#2


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