Question


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));

The error I get is

ERROR 1241 (21000): Operand should contain 1 column(s)

subquery which unions returns 2 columns. How can I fix this?

Was it helpful?

Solution

you lack FROM clause

SELECT track_id 
FROM   tableName
WHERE track_title 

so the full query would be

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
                                )
                );

another problem that will arise soon is the use of equal = sign on the result of the subquery, it is much safer to use IN than = because it will throw an exception if, for instance, the subquery will return more than one value. Example,

INSERT INTO People (Track_id_Reference)
SELECT track_id
FROM                                      -- <<== add tableName here
WHERE track_title IN (
                SELECT tracktitle
                FROM top100 .............
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top