SQL 删除存在值的行和具有该值的后续列
SQL Remove row where value exist and subsequent column that has the value
我正在尝试编写一个 SQL 语句,该语句首先仅获取第 1 列的不同值,然后我想将其与相同的 table 进行比较,但删除任何具有值的行在第 2 列
例如,当我写一个不包含 10142 的 select 时,FieldID 中的值 10142 只删除了第 1 行,但随后的 ID 列也没有行。
因此在屏幕截图中,我应该只能看到 ID 634 的所有结果,因为 633 的 FieldID 值为 10142。
我最初尝试将不同的 ID 值放入临时 table,然后在另一个 select 中过滤,其中 FieldID 不等于 10142,但仍然没有看到正确的结果。
这是我的查询:
SELECT DISTINCT id
INTO #TEMP
FROM tbl_WorkItemCustomLatest
ORDER BY ID ASC
SELECT a.*
FROM #TEMP
INNER JOIN dbo.tbl_WorkItemCustomLatest AS A ON #TEMP.id = A.id
WHERE A.FieldID != 10142
ORDER BY A.ID ASC
非常感谢任何帮助。
与NOT EXISTS
:
select t.* from tbl_WorkItemCustomLatest t
where not exists (
select 1 from tbl_WorkItemCustomLatest
where id = t.id and FieldId = 10142
)
或 NOT IN
:
select * from tbl_WorkItemCustomLatest
where id not in (select id from tbl_WorkItemCustomLatest where FieldId = 10142)
我正在尝试编写一个 SQL 语句,该语句首先仅获取第 1 列的不同值,然后我想将其与相同的 table 进行比较,但删除任何具有值的行在第 2 列
例如,当我写一个不包含 10142 的 select 时,FieldID 中的值 10142 只删除了第 1 行,但随后的 ID 列也没有行。
因此在屏幕截图中,我应该只能看到 ID 634 的所有结果,因为 633 的 FieldID 值为 10142。
我最初尝试将不同的 ID 值放入临时 table,然后在另一个 select 中过滤,其中 FieldID 不等于 10142,但仍然没有看到正确的结果。
这是我的查询:
SELECT DISTINCT id
INTO #TEMP
FROM tbl_WorkItemCustomLatest
ORDER BY ID ASC
SELECT a.*
FROM #TEMP
INNER JOIN dbo.tbl_WorkItemCustomLatest AS A ON #TEMP.id = A.id
WHERE A.FieldID != 10142
ORDER BY A.ID ASC
非常感谢任何帮助。
与NOT EXISTS
:
select t.* from tbl_WorkItemCustomLatest t
where not exists (
select 1 from tbl_WorkItemCustomLatest
where id = t.id and FieldId = 10142
)
或 NOT IN
:
select * from tbl_WorkItemCustomLatest
where id not in (select id from tbl_WorkItemCustomLatest where FieldId = 10142)