Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 删除同一列中的数据_Sql_Sql Server 2008 - Fatal编程技术网

Sql 删除同一列中的数据

Sql 删除同一列中的数据,sql,sql-server-2008,Sql,Sql Server 2008,我不知道如何解释我的问题,所以我展示了样本数据 我有下表 --------------------------------- |RecID |TypeOfData |Data | |21 |Label |Name | |21 |Data |Sam | |22 |Label |Name | |23 |Label |Name | |23 |Data

我不知道如何解释我的问题,所以我展示了样本数据

我有下表

---------------------------------
|RecID |TypeOfData  |Data       |
|21    |Label       |Name       |
|21    |Data        |Sam        |
|22    |Label       |Name       |
|23    |Label       |Name       |
|23    |Data        |Nimble     | 
我想删除所有那些没有数据的标签记录。 因此,在上面的示例中,需要删除RecID=22

请帮我做这个

Db是SQL Server 2008

这就是你想要的吗

delete t
    from t
    where t.typeofdata = 'Label' and
          not exists (select 1
                      from t t2
                      where t2.recid = t.recid and t2.typeofdata = 'Data'
                     );
“below”在关系表中没有任何含义,因为该表表示无序集。

这就是您想要的吗

delete t
    from t
    where t.typeofdata = 'Label' and
          not exists (select 1
                      from t t2
                      where t2.recid = t.recid and t2.typeofdata = 'Data'
                     );

“below”在关系表中并不表示任何内容,因为该表表示一个无序集。

可能是您可以使用一个子选择,其中Rec_id的计数为=1

  delete from my_table 
  where Rec_id in (select RecID from my_table 
                   group by RecId 
                   having count(*) =1)
 and TypeOdData = 'Label' 

您可以使用一个子选择,其中Rec_id的计数为=1

  delete from my_table 
  where Rec_id in (select RecID from my_table 
                   group by RecId 
                   having count(*) =1)
 and TypeOdData = 'Label' 

@湿婆见RecID 21和RecID 22。Rec ID dosent标签下有数据。@Shiva见RecID 21和RecID 22。记录ID在标签下面有数据。@NimbleFungus。你不接受这个答案有什么原因吗?你可以选择你喜欢的答案,但我很好奇。我选择其他答案,因为我不愿意不使用“不存在”。这是唯一的原因。谢谢我想把这两个都标记为答案。@NimbleFungus。这是一个遗憾,因为
不存在
可能比使用带有
COUNT(*)
@NimbleFungus的子查询的结果更好。你不接受这个答案有什么原因吗?你可以选择你喜欢的答案,但我很好奇。我选择其他答案,因为我不愿意不使用“不存在”。这是唯一的原因。谢谢我想把这两个都标记为答案。@NimbleFungus。这是一个遗憾,因为
不存在
可能比使用带有
COUNT(*)的子查询的结果更好。