以下SQL语句在我的数据库上执行正常:
SELECT * FROM tblKPIs AS k
INNER JOIN tblKeyPointLinks AS l ON k.KPIID = l.KPIID
INNER JOIN tblKeyPoints AS p ON p.KptID = l.KptID
INNER JOIN tblHistory AS h ON h.HistoryID = p.HistoryID
WHERE h.CaseNo = 50043;
然而,等效的Delete语句会在AS附近出现错误'语法?
DELETE FROM tblKPIs AS k
INNER JOIN tblKeyPointLinks AS l ON k.KPIID = l.KPIID
INNER JOIN tblKeyPoints AS p ON p.KptID = l.KptID
INNER JOIN tblHistory AS h ON h.HistoryID = p.HistoryID
WHERE h.CaseNo = 50043;
我可以在删除语句中使用联接吗?
如果不能如何执行上述删除?
修改
表tblKeyPointLinks是一个中间表,用于在tblKPI和tblKeyPoints之间建立多对多关系。因此,SELECT语句会多次返回tblKPI中的一些条目。这是为什么DELETE语句可能有问题?解决这个问题的最佳方法是什么?
答案 0 :(得分:5)
是的,您可以加入删除声明:
DELETE k FROM tblKPIs AS k
INNER JOIN tblKeyPointLinks AS l ON k.KPIID = l.KPIID
INNER JOIN tblKeyPoints AS p ON p.KptID = l.KptID
INNER JOIN tblHistory AS h ON h.HistoryID = p.HistoryID
WHERE h.CaseNo = 50043;
答案 1 :(得分:2)
你可以这样做:
DELETE FROM tblKPIs
WHERE id in (
SELECT id
FROM tblKPIs AS k
INNER JOIN tblKeyPointLinks AS l ON k.KPIID = l.KPIID
INNER JOIN tblKeyPoints AS p ON p.KptID = l.KptID
INNER JOIN tblHistory AS h ON h.HistoryID = p.HistoryID
WHERE h.CaseNo = 50043)
答案 2 :(得分:2)
成功的代码如下:
DELETE k
FROM tblKPIs k
INNER JOIN tblKeyPointLinks l ON k.KPIID = l.KPIID
INNER JOIN tblKeyPoints p ON p.KptID = l.KptID
INNER JOIN tblHistory h ON h.HistoryID = p.HistoryID
WHERE h.CaseNo = 50043;
显然DELETE
语句不喜欢使用关键字AS
只需省略AS
关键字,该语句就可以正常工作。