数据库
首页 > 数据库> > mysql – ERROR 1241(21000):执行查询时,操作数应包含1列

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

作者:互联网


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):操作数应包含1列

子查询哪个联合会返回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 .............

标签:sql,mysql,mysql-error-1241
来源: https://codeday.me/bug/20190729/1570218.html