简体   繁体   English

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

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

I am trying to Insert data from a table1 into table2 我正在尝试将表1中的数据插入表2中

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. table2中只有四列。

Syntax error, remove the ( ) from select . 语法错误,从select删除( )

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

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;

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 SELECT statement in the IN clause 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. IN子句中的嵌套SELECT语句返回两列,解析器将其视为操作数,这在技术上是正确的,因为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. 该查询所占的存储过程不仅被解析,而且还返回了预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM