0

I am trying to insert data using a subquery. I am only going to post my SELECT statement below.

SELECT PlaylistCode
FROM Playlists
WHERE Name_Of_Playlist = 'X'
AND 
(SELECT Code
  FROM Songs
  INNER JOIN Artists ON Artists.PageURL = Songs.PageURL
  WHERE Artist = 'X'
  LIMIT 200)

Error Code: 1242. Subquery returns more than 1 row 0.016 sec

6
  • In addition to missing EXISTS, your query is also missing aliases, which would have helped someone correct your syntax much faster. Commented Oct 16, 2019 at 1:51
  • 1
    @JS . . . You should explain what you want the code to do. Commented Oct 16, 2019 at 3:15
  • @GordonLinoff Here is my Partial EERD: EERD Here is my list SongCode where Artist = ‘X’: Artist X Commented Oct 16, 2019 at 4:03
  • @GordonLinoff In Songs Table PageURL is a PK. In Artists Table, PageURL is a Foreign Key referencing Songs.PageURL. In Playlists table, SongCode is FK referencing Songs.Code. In PlaylistSongs table, PlaylistCode is referencing Playlists.PlaylistCode. In Playlists table, UserCode is referencing Auth_User.UserCode. I want to insert all Songs from Songs table to PlaylistSongs table where artist = 'X' to PlaylistSongs table where Name_of_Playlist = ‘Y’. I hope you can understand it now. Commented Oct 16, 2019 at 4:03
  • Better version of EERD Link Commented Oct 16, 2019 at 4:18

2 Answers 2

1

Try to Below Query

SELECT PlaylistCode
FROM Playlists
WHERE exits 
(SELECT Code
  FROM Songs
  INNER JOIN Artists ON Artists.PageURL = Songs.PageURL
  WHERE Artist = 'X'
  LIMIT 200) and Name_Of_Playlist = 'X'
Sign up to request clarification or add additional context in comments.

Comments

0

Based on your requirements, you need to use select case then join corresponding tables

  INSERT INTO Playlists (PlaylistCode)
    SELECT CASE WHEN a.Artist = 'X' THEN 'X' ELSE s.Code END   
    FROM Songs s
    INNER JOIN Artists a ON a.PageURL = s.PageURL

7 Comments

I tried with exist & its only showing 1 result. My full query: INSERT INTO PlaylistSongs (PlaylistCode, SongCode) SELECT Playlists.PlaylistCode FROM Playlists WHERE Playlists.Name_Of_Playlist = 'Babbu Maan' AND EXISTS (SELECT Songs.Code FROM Songs INNER JOIN Artists ON Artists.PageURL = Songs.PageURL WHERE Artists.Artist = 'Babbu Maan' LIMIT 200)
removed limit 200, try again
still same issue.
i need your data and expected output, for us to solve this out
What I am trying to do is where ever the artist name = 'X' set the PlaylistCode to 'X' and insert all values to table PlaylistSongs
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.