ERROR 1241(21000):执行查询时,操作数应包含1列

时间:2012-11-25 06:37:04

标签: mysql sql mysql-error-1241


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列。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:7)

你缺少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比{{1}更安全因为,例如,如果子查询将返回多个值,它将抛出异常。例如,

=