有人可以帮我理解这个查询的错误:
DELETE FROM noteproject
INNER JOIN note ON noteproject.noteID = note.noteID
INNER JOIN person ON note.personID = person.personID
WHERE noteID = '#attributes.noteID#'
AND personID = '#attributes.personID#'
答案 0 :(得分:9)
我目前没有数据库来测试我在说什么,但here's a reference to the mysql docs以你的案例为例:
您可以在DELETE语句中指定多个表,以根据WHERE子句中的特定条件从一个或多个表中删除行。
还有:
1)对于第一个多表语法,只删除FROM子句之前列出的表中的匹配行 2)对于第二个多表语法,只删除FROM子句(USING子句之前)中列出的表中的匹配行。
效果是您可以同时从多个表中删除行和有另外的表只用于搜索:
1)
DELETE t1, t2
FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
2)
DELETE FROM t1, t2
USING t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
这些语句在搜索要删除的行时使用所有三个表,但仅删除表t1和t2中的匹配行。
答案 1 :(得分:8)
您尝试使用的内容不起作用的原因是因为MySQL doesn't support join syntax in the delete statement in the manner you tried。
使用:
DELETE FROM NOTEPROJECT
WHERE noteID = '#attributes.noteID#'
AND note_id IN (SELECT n.note_id
FROM NOTE n
WHERE n.personID = '#attributes.personID#')
......或使用EXISTS:
DELETE FROM NOTEPROJECT
WHERE noteID = '#attributes.noteID#'
AND EXISTS (SELECT NULL
FROM NOTE n
WHERE n.note_id = note_id
AND n.personID = '#attributes.personID#')
答案 2 :(得分:3)
使用MySQL 5.1.41进行测试:
DELETE np
FROM noteproject np
INNER JOIN note n ON np.noteID = n.noteID
INNER JOIN person p ON n.personID = p.personID
WHERE n.noteID = '#attributes.noteID#'
AND p.personID = '#attributes.personID#';
说明:
noteID
子句中使用表名或表别名来限定personID
和WHERE
,否则MySQL会抱怨它们不明确(因为该列存在于多个一桌)。DELETE
。有几种不同的形式,但您没有匹配任何合法的语法形式。答案 3 :(得分:2)
应该是:
DELETE noteproject FROM noteproject
INNER JOIN ...
或者,你可以写:
DELETE FROM noteproject USING noteproject
INNER JOIN ...
答案 4 :(得分:0)
尝试WHERE部分
tblname.noteID = '#attributes.noteID#' AND tblname.personID = '#attributes.personID#'