以另一列为条件删除具有特定 ID 的行组
Delete group of rows with specific ID conditional on another column
我有一个 table,其中包含几千个条目,如下所示:
ID Data Type Amount
1 Start
1 Money 13.45
1 Money 3.79
1 Money 46.82
1 END
2 Start
2 Money 26.24
2 END
3 Start
3 END
我想删除任何看起来像上面示例中 ID=3 的 ID 组,其中只有 Start/END 但没有钱。我正在尝试使用内部联接来识别这些行,但在形成 select 语句时遇到问题。我如何 select 符合此描述的行?
简单选项:
Delete from MyTable
Where ID not in (Select ID from MyTable where [Data Type] = 'Money')
假设你的 table 被称为测试,下面应该为你做。
DELETE FROM test
WHERE id NOT IN (
SELECT id FROM test WHERE data = 'Money');
我有一个 table,其中包含几千个条目,如下所示:
ID Data Type Amount
1 Start
1 Money 13.45
1 Money 3.79
1 Money 46.82
1 END
2 Start
2 Money 26.24
2 END
3 Start
3 END
我想删除任何看起来像上面示例中 ID=3 的 ID 组,其中只有 Start/END 但没有钱。我正在尝试使用内部联接来识别这些行,但在形成 select 语句时遇到问题。我如何 select 符合此描述的行?
简单选项:
Delete from MyTable
Where ID not in (Select ID from MyTable where [Data Type] = 'Money')
假设你的 table 被称为测试,下面应该为你做。
DELETE FROM test
WHERE id NOT IN (
SELECT id FROM test WHERE data = 'Money');