75

I am trying to Insert data from a table1 into table2

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

Key for table2 is student_id.

Assume that there are not any duplicates.

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

There are only four columns in table2.

Kumaran Senapathy
  • 1,233
  • 4
  • 18
  • 30

3 Answers3

165

Syntax error, remove the ( ) from select.

insert into table2 (name, subject, student_id, result)
select name, subject, student_id, result
from table1;
David
  • 3,388
  • 2
  • 21
  • 25
27

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

insert into table2 (Name, Subject, student_id, result)
select Name, Subject, student_id, result
from table1;
fthiella
  • 48,073
  • 15
  • 90
  • 106
  • OperationalError: (MySQLdb._exceptions.OperationalError) (1241, 'Operand should contain 1 column(s)') [SQL: INSERT INTO meedan_newsartcile_details (identifier, `@context`, `@type`, `datePublished`, url, author, `claimReviewed`, `text`, image, `reviewRating`, verdict, I am getting error, how to solve? article_title, type, `ratingValue`, `bestRating`, author_name, author_url, `ID`, news_source) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)] – SUSHMA KUMARI Apr 05 '21 at 16:21
  • @SUSHMAKUMARI I think you've put variable names in the insertion field list spec: ```@context``` and ```@type``` are not field names are they? Rather they are data you wish to write into whatever those fields are actually called – cherry Feb 21 '22 at 20:00
9

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.

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.

Tomm
  • 1,021
  • 2
  • 14
  • 34
David A. Gray
  • 1,039
  • 12
  • 19