使用联接删除查询

时间:2012-09-08 09:04:37

标签: mysql sql join sql-delete

我正在运行一个要从表中删除的查询。

delete from sps
inner join str on str.studentid = sps.studentid 
where str.studentid like '%2012%psy%' 
  and str.semesterid=2 
inner join papers on papers.id = sps.paperid 
where papers.c_id=10 
  and papers.p_semid=2

我不知道为什么它没有显示任何结果并给我一个错误。

错误是:

  

#1064 - 您的SQL语法出错;检查手册       对应于您的MySQL服务器版本,以便在附近使用正确的语法       'inner join str on str.studentid = sps.studentid其中str.studentid喜欢'%2012%ps'       在第2行

2 个答案:

答案 0 :(得分:1)

Mysql DELETE syntax所述,在查询结尾处将两个WHERE子句的两个谓词移动到一个WHERE子句中。类似的东西:

DELETE s 
FROM sps s
INNER JOIN str ON str.studentid = s.studentid 
INNER JOIN papers ON papers.id = s.paperid 
WHERE str.studentid LIKE '%2012%psy%' 
  AND str.semesterid = 2 
  AND papers.c_id = 10 
  AND papers.p_semid = 2

答案 1 :(得分:0)

您可以在一次查询中使用WHERE子句一次 试试这个查询

DELETE sps FROM sps
INNER JOIN str ON str.studentid=sps.studentid
INNER JOIN papers ON papers.id=sps.paperid 
WHERE papers.c_id=10 AND papers.p_semid=2
AND str.studentid LIKE '%2012%psy%' AND str.semesterid=2 

OR

DELETE FROM sps, str, papers 
USING sps 
      INNER JOIN str ON str.studentid=sps.studentid
      INNER JOIN papers ON papers.id=sps.paperid 
WHERE   papers.c_id=10 
    AND papers.p_semid=2
    AND str.studentid LIKE '%2012%psy%' 
    AND str.semesterid=2