试图让我的删除声明工作

时间:2013-06-14 07:38:56

标签: sql sql-server database sql-delete

我想根据另一个表中的列来删除表中的行:

Table1:            Table2:
value              value, i

如果table2.i小于1,则从table1中删除相应的行(但保留在table2中)。

问题是价值不是唯一的,所以如果我有这个例子:

 Table1            table2
+-------+        +-----------+
| value |        | value | i |
+-------+        +-----------+
|   5   |        |  5    | 0 |
|   3   |        |  5    | 3 |
+-------+        |  3    | 0 |
                 |  3    | 0 |
                 +-----------+

值3应该从table1中删除(因为table2中的所有匹配都有i< 1)但是值5应该保留(因为表2中的i = 3行)

到目前为止我的代码(不起作用):

DELETE FROM Table1, Table2
WHERE (SELECT MIN(Table2.i) FROM Table1, Table2
       WHERE Table1.value = Table2.value) < 1;

问题是:由于我的子查询为所有行返回min,所以一切都被删除。

我不能在子查询中使用“group by”,因为那时我的比较是不允许的。

4 个答案:

答案 0 :(得分:2)

试试这个:

DELETE FROM Table1 
WHERE NOT EXISTS(SELECT 1  
                   FROM Table2          
                  WHERE Table2.i > 0 
                    AND Table2.value = Table1.value)

答案 1 :(得分:1)

我不知道你为什么使用min,而是你应该使用max:

试试这个

     DELETE FROM Table1
   WHERE Table1.value1 = Table2.value1
   and (SELECT MAX(Table2.i) FROM Table2
   WHERE Table1.value1 = Table2.value1) < 1;

答案 2 :(得分:1)

好的,我首先编写一个选择要删除的行的查询,

SELECT
            *
    FROM
       Table1
    EXCEPT
    (
    SELECT
                t1.value
        FROM
                Table1 t1
            JOIN
                Table2 t2
                    ON t2.value = t1.value
        WHERE
                t2.i > 0
    );

See Fiddle

然后将SELECT更改为DELETE

DELETE Table1
    FROM
       Table1 t1
    WHERE
       t1.value NOT IN
    (
    SELECT
                t1.value
        FROM
                Table1 t1
            JOIN
                Table2 t2
                    ON t2.value = t1.value
        WHERE
                t2.i > 0
    );

See Fiddle

答案 3 :(得分:0)

怎么样:

delete from table1 where value in
    (select value from table2 group by value having max(i) < 1)

按值对表格2进行分组,并使用having检测最大值小于1的位置,允许您从表格1中选择正确的删除值。

having基本上是一个where子句,它在聚合后发挥,因此可以与max一起使用,依此类推。

这是一个显示其实际效果的脚本:

DROP TABLE TABLE1;
DROP TABLE TABLE2;
CREATE TABLE TABLE1 (VALUE INTEGER);
CREATE TABLE TABLE2 (VALUE INTEGER, I INTEGER);
INSERT INTO TABLE1 VALUES (5);
INSERT INTO TABLE1 VALUES (3);
INSERT INTO TABLE2 VALUES (5, 0);
INSERT INTO TABLE2 VALUES (5, 3);
INSERT INTO TABLE2 VALUES (3, 0);
INSERT INTO TABLE2 VALUES (3, 0);

SELECT * FROM TABLE1;
SELECT * FROM TABLE2;

DELETE FROM TABLE1 WHERE VALUE IN
  (SELECT VALUE FROM TABLE2 GROUP BY VALUE HAVING MAX(I) = 0);

SELECT * FROM TABLE1;
SELECT * FROM TABLE2;

此脚本的输出如下所示。首先,设置所有表:

DROP TABLE TABLE1; DROP TABLE TABLE2;
    TABLE1 DROPPED
    TABLE2 DROPPED
CREATE TABLE TABLE1 (VALUE INTEGER);
    TABLE1 CREATED
CREATE TABLE TABLE2 (VALUE INTEGER, I INTEGER);
    TABLE2 CREATED
INSERT INTO TABLE1 VALUES ((5), (3));
    INSERTED 2 ROWS
INSERT INTO TABLE2 VALUES ((5, 0), (5, 3), (3,0), 3, 0));
    INSERTED 4 ROWS

并显示它们以确保它们符合预期:

SELECT * FROM TABLE1; SELECT * FROM TABLE2;
    VALUE
    -----
      5
      3
    VALUE   I
    -----   -
      5     0
      5     3
      3     0
      3     0

然后运行命令以删除相关行:

DELETE FROM TABLE1 WHERE VALUE IN                          
  (SELECT VALUE FROM TABLE2 GROUP BY VALUE HAVING MAX(I) = 0);
    DELETED 1 ROW

你可以看到3`行已根据需要从表1中消失:

SELECT * FROM TABLE1; SELECT * FROM TABLE2;
    VALUE
    -----
      5
    VALUE   I
    -----   -
      5     0
      5     3
      3     0
      3     0