DELETE中多inner join 的一点疑问
表结构:
表College
CollegeID,CollegeName
表Major
MajorID,MajorName,CollegeID
表Class
ClassID,ClassName,MajorID
表 StudentInfo
ClassID ……(其余省略)
现在要根据CollegeID来删除对应的 College,Major,Class,StudentInfo
我写的SQL
delete from StudentInfo from StudentInfo
inner join Class on Class.ClassID=StudentInfo.ClassID
inner join Major on Major.MajorID=Class.MajorID
where Major.CollegeID=?
--StudentInfo,Class 先join啊,按照你join的顺序依次执行
delete StudentInfo from StudentInfo where exists(
select 1 from Class
inner join Major on Major.MajorID=Class.MajorID
where Class.ClassID=StudentInfo.ClassID and Major.CollegeID=?)