Solution to quot;subquery returns more than 1 rowquot; error(“子查询返回多于1行的解决方案错误)
问题描述
我有一个返回多行的查询,还有另一个查询,我想在其中将条件设置为来自这些多行的值之一,所以基本上我希望子查询看起来像这样:
I have one query that returns multiple rows, and another query in which I want to set criteria to be either one of values from those multiple rows , so basicly I want the subquery to look something like this:
select *
from table
where id= (multiple row query);
其中 multiple row query
返回多行.因此,如果这些行的值是 1、2、3,那么我想将 id 设置为 1 或 2 或 3.
Where multiple row query
returns multiple rows. So if the values from those rows are 1,2,3 then I want to set id to be 1 or 2 or 3.
推荐答案
=
可以在子查询只返回 1 个值时使用.
=
can be used when the subquery returns only 1 value.
当子查询返回超过 1 个值时,您将不得不使用 IN
:
When subquery returns more than 1 value, you will have to use IN
:
select *
from table
where id IN (multiple row query);
例如:
SELECT *
FROM Students
WHERE Marks = (SELECT MAX(Marks) FROM Students) --Subquery returns only 1 value
SELECT *
FROM Students
WHERE Marks IN
(SELECT Marks
FROM Students
ORDER BY Marks DESC
LIMIT 10) --Subquery returns 10 values
这篇关于“子查询返回多于1行"的解决方案错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“子查询返回多于1行"的解决方案错误


- SQL 临时表问题 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01