从WHERE IN删除,但有2个字段,没有Id

Mat*_*ima 0 sql sql-server delete-row sql-delete

我有这张桌子:

StudentId | ConfigurationTypeId
1         | 1
1         | 2
1         | 3
2         | 1
2         | 2
Run Code Online (Sandbox Code Playgroud)

我想删除此表中的所有记录 ConfigurationTypeId = 3

所以我提出了这个问题:

DELETE FROM [StudentConfigurationType] WHERE StudentId IN
  (SELECT StudentId FROM [StudentConfigurationType] SCT 
  INNER JOIN [Student] S ON S.Id = SCT.StudentId
  WHERE SCT.ConfigurationTypeId = 3 AND S.RegisteredDate < '2014-09-18')
Run Code Online (Sandbox Code Playgroud)

但是,因为这个表没有Id,所以StudentId会进来,它会从表中删除所有记录.怎么能做这样的事情:

DELETE FROM [StudentConfigurationType] WHERE StudentId AND ConfigurationTypeId IN
  (SELECT StudentId, ConfigurationTypeId FROM [StudentConfigurationType] SCT
  INNER JOIN [Student] S ON S.Id = SCT.StudentId
  WHERE SCT.ConfigurationTypeId = 3 AND S.RegisteredDate < '2014-09-18')
Run Code Online (Sandbox Code Playgroud)

Mul*_*ync 5

你可以试试这个:

DELETE FROM [StudentConfigurationType] WHERE (StudentId, ConfigurationTypeId) IN
  (SELECT StudentId, ConfigurationTypeId FROM [StudentConfigurationType] SCT
  INNER JOIN [Student] S ON S.Id = SCT.StudentId
  WHERE SCT.ConfigurationTypeId = 3 AND S.RegisteredDate < '2014-09-18');
Run Code Online (Sandbox Code Playgroud)

或这个:

DELETE FROM [StudentConfigurationType] 
WHERE StudentId IN (SELECT StudentId 
                      FROM [StudentConfigurationType] SCT
                           INNER JOIN [Student] S ON S.Id = SCT.StudentId
                      WHERE S.RegisteredDate < '2014-09-18')
 AND ConfigurationTypeId = 3;
Run Code Online (Sandbox Code Playgroud)

或这个:

DELETE FROM [StudentConfigurationType] st
WHERE EXISTS (SELECT 1 FROM [Student] S WHERE S.Id = ST.StudentId AND S.RegisteredDate < '2014-09-18')
 AND ConfigurationTypeId = 3;
Run Code Online (Sandbox Code Playgroud)