使用内部连接删除一行数据

9rnv2umw  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(245)

这个问题在这里已经有答案了

vs where子句的内部连接(12个答案)
两年前关门了。
我试图使用内部联接从表中删除一行数据,但是我的代码似乎不起作用。我首先写了一个select语句,以确保选择了正确的数据:

SELECT * FROM tblPlaylist_Tracks
INNER JOIN tblPlaylist ON tblPlaylist_Tracks.PlaylistID = tblPlaylist.PlaylistID
INNER JOIN tblTrack ON tblPlaylist_Tracks.TrackID = tblTrack.TrackID
WHERE Playlist_Name =  "x" AND Track_Name =  "y";

然后我用delete语句替换了select语句,但是这在第2行引发了一个错误:

DELETE FROM tblPlaylist_Tracks
INNER JOIN tblPlaylist ON tblPlaylist_Tracks.PlaylistID = tblPlaylist.PlaylistID
INNER JOIN tblTrack ON tblPlaylist_Tracks.TrackID = tblTrack.TrackID
WHERE Playlist_Name =  "x" AND Track_Name =  "y";

我试图在delete-from语句之间指定要删除的内容,但是这似乎不起作用,因为查询不再将delete识别为命令,请尽可能地提供帮助。非常感谢

vsdwdz23

vsdwdz231#

我会这样写:

DELETE pt
    FROM tblPlaylist_Tracks pt INNER JOIN 
         tblPlaylist p
         ON pt.PlaylistID = p.PlaylistID INNER JOIN
         tblTrack t
         ON pt.TrackID = t.TrackID
WHERE p.Playlist_Name = 'x' AND t.Track_Name =  'y';

这对你有用吗?
我不知道你说的“不再承认”是什么意思 DELETE 作为命令”。使用时 DELETEJOINFROM .

h7wcgrx3

h7wcgrx32#

这是有道理的——sql会将您的请求解释为从三个表中删除一个请求,或者从从您的联接构建的“虚拟”表中删除一个请求。
我将尝试将您的工作选择 Package 到删除:

delete from tblPlaylist_Tracks 
where primaryKey not in (
  SELECT primaryKey FROM tblPlaylist_Tracks
  INNER JOIN tblPlaylist ON tblPlaylist_Tracks.PlaylistID = 
  tblPlaylist.PlaylistID
  INNER JOIN tblTrack ON tblPlaylist_Tracks.TrackID = tblTrack.TrackID
  WHERE Playlist_Name =  "x" AND Track_Name =  "y";
)

当然,“primarykey”将是tblplaylist\u轨迹的独特标识符

相关问题