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.
asked Apr 4, 2013 at 19:43
Syntax error, remove the ( )
from select
.
insert into table2 (name, subject, student_id, result)
select name, subject, student_id, result
from table1;
answered Apr 4, 2013 at 19:45
1
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;
answered Apr 4, 2013 at 19:44
fthiellafthiella
47.9k15 gold badges89 silver badges105 bronze badges
2
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,0212 gold badges14 silver badges34 bronze badges
answered Mar 5, 2018 at 5:46
0
You cannot have multiple columns being returned in a subquery like that, so you have several ways that you would have rewrite this query to work.
Either you can unpivot the data in the team
table so you are only returning one column:
select *
from players
where sport='football'
and position='DEF'
and pname!='Binoy Dalal'
and pname not in (select player1
from team where sap='60003100009'
union all
select player2
from team where sap='60003100009'
union all
select player3
from team where sap='60003100009'
union all
select player4
from team where sap='60003100009'
union all
select player5
from team where sap='60003100009'
union all
select player6
from team where sap='60003100009'
union all
select player7
from team where sap='60003100009'
union all
select player8
from team where sap='60003100009')
order by price desc;
Or you can use a NOT EXISTS
query:
select *
from players p
where sport='football'
and position='DEF'
and pname!='Binoy Dalal'
and not exists (select *
from team t
where sap='60003100009'
AND
(
p.pname = t.player1 OR
p.pname = t.player2 OR
p.pname = t.player3 OR
p.pname = t.player4 OR
p.pname = t.player5 OR
p.pname = t.player6 OR
p.pname = t.player7 OR
p.pname = t.player8
))
order by price desc;
Or you would have to use multiple WHERE
filters on the player name:
select *
from players
where sport='football'
and position='DEF'
and pname!='Binoy Dalal'
and pname not in (select player1
from team where sap='60003100009')
and pname not in (select player2
from team where sap='60003100009')
and pname not in (select player3
from team where sap='60003100009')
and pname not in (select player4
from team where sap='60003100009')
and pname not in (select player5
from team where sap='60003100009')
and pname not in (select player6
from team where sap='60003100009')
and pname not in (select player7
from team where sap='60003100009')
and pname not in (select player8
from team where sap='60003100009')
order by price desc;
However, ideally you should consider normalizing the team
table so you have one column with the player name and another column that assigns them a player number. Similar to this:
create table team
(
player varchar(50),
playerNumber int
);
Then when you are searching the team data you only have to join on one column instead of 8 different columns.
I want to update an auto increment b_id based on column house_id example:
id house_id b_id
+-------------------+
| 1 | H1 | 1 |
| 2 | H1 | 2 |
| 3 | H1 | 3 |
| 4 | H2 | 1 |
| 5 | H3 | 1 |
+-------------------+
Tried with this code and successfully generated the b_id but does not update the table.
SELECT t.id,
house_id,
(SELECT count(*)
FROM House
WHERE house_id = t.house_id
AND id <= t.id
) AS b_id
FROM House t
So i tried with this code:
UPDATE House
SET b_id = (SELECT t.id,
house_id,
(SELECT count(*)
FROM House
WHERE house_id = t.house_id
AND id <= t.id
) AS b_id
FROM House t);
But there is error #1241 — Operand should contain 1 column(s).
I explore on internet, but do not really understand and cannot related with my sql code. Any idea?
Вопрос:
INSERT INTO People(Track_id_Reference)
SELECT track_id
FROM Tracks
WHERE track_title IN (SELECT tracktitle
FROM top100
WHERE artist IN (SELECT p.People_name, t.artist
FROM People AS p
RIGHT JOIN top100 AS t
ON
p.People_name=t.artist
UNION DISTINCT
SELECT p.People_name, t.artist
FROM People AS p
LEFT JOIN top100 AS t
ON
p.People_name=t.artist));
Ошибка, которую я получаю,
ERROR 1241 (21000): Operand should contain 1 column(s)
подзапрос, объединения которого возвращают 2 столбца. Как я могу это исправить?
Лучший ответ:
вам не хватает FROM
предложение
SELECT track_id
FROM tableName
WHERE track_title
поэтому полный запрос будет
INSERT INTO People (Track_id_Reference)
SELECT track_id
FROM -- <<== add tableName here
WHERE track_title = (
SELECT tracktitle
FROM top100
WHERE artist = (
SELECT p.People_name,
t.artist
FROM People AS p
RIGHT JOIN top100 AS t
ON p.People_name = t.artist
UNION
DISTINCT
SELECT p.People_name,
t.artist
FROM People AS p
LEFT JOIN top100 AS t
ON p.People_name = t.artist
)
);
еще одна проблема, которая возникнет в ближайшее время, – это использование знака =
в результате подзапроса, гораздо безопаснее использовать IN
, чем =
, потому что он будет генерировать исключение, если, например, в подзапросе будет возвращено более одного значения. Пример,
INSERT INTO People (Track_id_Reference)
SELECT track_id
FROM -- <<== add tableName here
WHERE track_title IN (
SELECT tracktitle
FROM top100 .............