使用子查询将数据插入表中

时间:2019-10-16 01:45:24

标签: mysql sql

我正在尝试使用子查询插入数据。我只会在下面发布我的SELECT声明。

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)
  

错误代码:1242。子查询返回的行多于0.016秒

2 个答案:

答案 0 :(得分:1)

尝试以下查询

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'

答案 1 :(得分:0)

根据您的要求,您需要先使用select case然后使用join相应的表

  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