How to solve "subquery returns more than 1 row" error
P粉235202573
P粉235202573 2023-08-21 17:36:57
0
2
467
<p>I have a query that returns multiple rows, and another query where I want to set the condition to be a value for any one of these multiple rows, so basically I want the subquery to look like this: </p> <pre class="brush:php;toolbar:false;">select * from table where id= (multi-row query);</pre> <p>Where <code>Multi-row query</code> returns multiple rows. So if the values ​​of these rows are 1, 2, 3, then I want the id to be set to 1 or 2 or 3. </p>
P粉235202573
P粉235202573

reply all(2)
P粉310754094

You can use in():

select * 
from table
where id in (多行查询)

Or use connection:

select distinct t.* 
from source_of_id_table s
join table t on t.id = s.t_id
where <source_of_id_table的条件>

Connections are never a worse choice in terms of performance, and depending on the circumstances and the database you are using, may provide better performance.

P粉386318086

= can be used when a subquery returns only one value.

When a subquery returns multiple values, you need to use IN:

select * 
from table
where id IN (multiple row query);

For example:

SELECT *
FROM Students
WHERE Marks = (SELECT MAX(Marks) FROM Students)   --子查询只返回一个值

SELECT *
FROM Students
WHERE Marks IN 
      (SELECT Marks 
       FROM Students 
       ORDER BY Marks DESC
       LIMIT 10)                       --子查询返回10个值
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!