MySQL 错误 1241:操作数应包含 1 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15820288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:12:48  来源:igfitidea点击:

MySQL error 1241: Operand should contain 1 column(s)

mysqlsqlselectinsertmysql-error-1241

提问by Kumaran Senapathy

I am trying to Insert data from a table1 into table2

我正在尝试将 table1 中的数据插入 table2

insert into table2(Name,Subject,student_id,result)
select (Name,Subject,student_id,result)
from table1;

Key for table2 is student_id.

table2 的键是 student_id。

Assume that there are not any duplicates.

假设没有任何重复。

I get the error: MySQL error 1241: Operand should contain 1 column(s)

我收到错误: MySQL error 1241: Operand should contain 1 column(s)

There are only four columns in table2.

表 2 中只有四列。

回答by David

Syntax error, remove the ( )from select.

语法错误,删除( )from select

insert into table2 (name, subject, student_id, result)
select name, subject, student_id, result
from table1;

回答by fthiella

Just remove the (and the )on your SELECT statement:

只是删除()你的SELECT语句:

insert into table2 (Name, Subject, student_id, result)
select Name, Subject, student_id, result
from table1;

回答by David A. Gray

Another way to make the parser raise the same exception is the following incorrect clause.

使解析器引发相同异常的另一种方法是以下不正确的子句。

SELECT r.name
FROM roles r
WHERE id IN ( SELECT role_id ,
                     system_user_id
                 FROM role_members m
                 WHERE r.id = m.role_id
                 AND m.system_user_id = intIdSystemUser
             )

The nested SELECTstatement in the INclause returns two columns, which the parser sees as operands, which is technically correct, since the id column matches values from but one column (role_id) in the result returned by the nested select statement, which is expected to return a list.

子句中的嵌套SELECT语句IN返回两列,解析器将其视为操作数,这在技术上是正确的,因为 id 列仅匹配嵌套 select 语句返回的结果中的一列 (role_id) 中的值,该语句预期返回一个列表。

For sake of completeness, the correct syntax is as follows.

为完整起见,正确的语法如下。

SELECT r.name
FROM roles r
WHERE id IN ( SELECT role_id
                 FROM role_members m
                 WHERE r.id = m.role_id
                 AND m.system_user_id = intIdSystemUser
             )

The stored procedure of which this query is a portion not only parsed, but returned the expected result.

此查询所在的存储过程不仅被解析,而且返回了预期的结果。