【问题标题】:SQL Server : delete a record only if it is associated with all the groups of the tableSQL Server:仅当记录与表的所有组关联时才删除记录
【发布时间】:2015-06-25 12:29:07
【问题描述】:

这个问题是关于一个涉及两个表的DELETE 查询。

我有两个表,分别名为 TableOneTableTwo

TableTwo 中的列与 TableOne 中的列相同。

CREATE TABLE TABLEONE
(
    ColumnOne INT,
    ColumnTwo INT
);

CREATE TABLE TABLETWO
(
    ColumnOne INT,
    ColumnTwo INT
 );

HERE IS SQLFIDDLE:

但它们与每个表包含的记录数不同。

ColumnOneColumnTwo 这两个表中都可以包含重复值。

我想根据 ColumnTwo 的值删除 TableTwo 中的记录。

例如,假设TableTwo中有以下记录

ColumnOne  ColumnTwo
1          200
1          300
1          400
2          200
2          100

并且TableOne中有以下记录(包含需要删除的记录)

ColumnOne  ColumnTwo
1          200
2          200
2          100

根据我的要求,只删除表TableTwoColumnTwo中包含200条的记录。

我写了下面的查询来删除TableTwo中的记录

DELETE FROM TableTwo
WHERE ColumnOne IN  (SELECT l.ColumnOne 
                     FROM TableOne l 
                     WHERE l.ColumnTwo = TableTwo.ColumnTwo )

此查询同时删除包含 200 和 100 的记录。但我只需要删除包含 200 的记录,因为它与 TableTwo 中所有唯一的 ColumnOne 值相关联。

删除要求:TableTwo - 指定的ColumnTwo 值必须与ColumnOne 的所有唯一值相关联。

有人可以帮我实现吗?

【问题讨论】:

  • 如果Table2中的记录包含“3, 200”,是否也会被删除?

标签: sql sql-server delete-row


【解决方案1】:

TableOne 中与所有组关联的 Column2 值由以下人员找到:

select column2
from TableOne
group by column2
having count(*) = (select count(distinct column1 from tableone));

要从TableTwo 中删除这些值:

delete from TableTwo t2
    where t2.column2 in (select column2
                         from TableOne
                         group by column2
                         having count(*) = (select count(distinct column1 from tableone))
                        );

注意:如果可以复制整行,则count(*) 应为count(distinct column1)

【讨论】:

    【解决方案2】:

    试试EXCEPT。通过此操作,您将从 table2 中删除匹配的行,并仅在 table1 中保留那些没有相应关联的行。所以NOT IN 只取那些删除的行:

    DECLARE @TABLEONE TABLE
        (
          ColumnOne INT ,
          ColumnTwo INT
        );
    
    
    DECLARE @TABLETWO TABLE
        (
          ColumnOne INT ,
          ColumnTwo INT
        );
    
    
    INSERT  INTO @TABLEONE
    VALUES  ( 1, 200 ),
            ( 2, 200 ),
            ( 2, 100 )
    
    INSERT  INTO @TABLETWO
    VALUES  ( 1, 200 ),
            ( 1, 300 ),
            ( 1, 400 ),
            ( 2, 200 ),
            ( 2, 100 )
    
    
    DELETE  FROM @TABLETWO
    WHERE   ColumnOne NOT IN ( SELECT   ColumnOne
                               FROM     ( SELECT    * FROM      @TABLETWO
                                          EXCEPT
                                          SELECT    * FROM      @TABLEONE
                                        ) t )
    

    【讨论】:

      【解决方案3】:
      Try this one:
      
      DELETE FROM @TABLETWO
      WHERE ColumnTwo IN (SELECT ColumnTwo FROM @TABLETWO
      GROUP BY ColumnTwo
      HAVING COUNT(DISTINCT ColumnOne) > 1)
      AND ColumnOne IN (SELECT ColumnOne FROM @TABLEONE)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-07
        • 1970-01-01
        相关资源
        最近更新 更多