如果一个表A  其中有两个字段  id 和 del
都不是主键,我想作这样的操作:
如果id=1的记录 del都是1 则 删除所有id=1的记录 .
否则都不删除
我这样写了 
delete from A
where id=1
and (select count(*) from A where id=1)
=(select count(*) from A where id=1 and del=1)这样写 Mysql会报错
正确的应该怎么写 ?

解决方案 »

  1.   

    delete from 表A
    where id in (
    select id from 表A t where not exists (select 1 from 表A where id=t.id and del!=1)
    )
      

  2.   

    就可以在where语句中 引用被更新表了 ?
      

  3.   

    用别名,另外sql可以简单一些。
    delete from A a where not exists (select * from a where a.id=1 and a.del<>1)
      

  4.   


    delete from table1 a where not exists (select * from a where a.code=1 and a.del<>1)
    Error Code : 1064
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a where not exists (select * from a where a.code=1 and a.del<>1)' at line 1
    (0 ms taken)
      

  5.   


       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  6.   

    表名 table1id  int 主键
    name  varchar
    code int
    del int
    插入三条语句insert into table1 value(1,'jia',1,1) 
    insert into table1 value(2,'jia',1,1) 
    insert into table1 value(3,'jia',1,0)
    执行delete from table1
    where code in (
    select code from table1 t where not exists (select 1 from t where code=t.code and del!=1)
    )
    Error Code : 1064
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a where not exists (select * from a where a.code=1 and a.del<>1)' at line 1
    (0 ms taken)
     
     
      

  7.   

    如果
    插入三条语句 是这样    insert into table1 value(1,'jia',1,1)  
    insert into table1 value(2,'jia',1,1)  
    insert into table1 value(3,'jia',1,1)
    执行 
    delete from table1
    where code in (
    select code from table1 t where not exists (select 1 from t where code=t.code and del!=1)
    )
    结果应该是三条全部删除
      

  8.   

    mysql> create table table1(
        -> id  int primary key,
        -> name  varchar(10),
        -> code int,
        -> del int
        -> );
    Query OK, 0 rows affected (0.09 sec)mysql> insert into table1 value(1,'jia',1,1)  ;
    Query OK, 1 row affected (0.28 sec)mysql> insert into table1 value(2,'jia',1,1)  ;
    Query OK, 1 row affected (0.30 sec)mysql> insert into table1 value(3,'jia',1,0);
    Query OK, 1 row affected (0.28 sec)mysql> select * from table1;
    +----+------+------+------+
    | id | name | code | del  |
    +----+------+------+------+
    |  1 | jia  |    1 |    1 |
    |  2 | jia  |    1 |    1 |
    |  3 | jia  |    1 |    0 |
    +----+------+------+------+
    3 rows in set (0.00 sec)mysql> insert into table1 value(1,'jia2',2,1) ;
    ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
    mysql> insert into table1 value(2,'jia2',2,1) ;
    ERROR 1062 (23000): Duplicate entry '2' for key 'PRIMARY'
    mysql> insert into table1 value(3,'jia2',2,1);
    ERROR 1062 (23000): Duplicate entry '3' for key 'PRIMARY'
    mysql> insert into table1 values(1,'jia2',2,1) ;
    ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
    mysql> insert into table1 values(2,'jia2',2,1) ;
    ERROR 1062 (23000): Duplicate entry '2' for key 'PRIMARY'
    mysql> insert into table1 values(3,'jia2',2,1);
    ERROR 1062 (23000): Duplicate entry '3' for key 'PRIMARY'
    mysql> insert into table1 values(11,'jia2',2,1) ;
    Query OK, 1 row affected (0.06 sec)mysql> insert into table1 values(12,'jia2',2,1) ;
    Query OK, 1 row affected (0.03 sec)mysql> insert into table1 values(13,'jia2',2,1);
    Query OK, 1 row affected (0.03 sec)mysql> select * from table1;
    +----+------+------+------+
    | id | name | code | del  |
    +----+------+------+------+
    |  1 | jia  |    1 |    1 |
    |  2 | jia  |    1 |    1 |
    |  3 | jia  |    1 |    0 |
    | 11 | jia2 |    2 |    1 |
    | 12 | jia2 |    2 |    1 |
    | 13 | jia2 |    2 |    1 |
    +----+------+------+------+
    6 rows in set (0.00 sec)mysql> delete a from table1 a, (
        -> select code from table1 t where not exists (select 1 from table1 where code=t.code and del!=1)
        -> ) b
        -> where a.code=b.code;
    Query OK, 3 rows affected (0.08 sec)mysql> select * from table1;
    +----+------+------+------+
    | id | name | code | del  |
    +----+------+------+------+
    |  1 | jia  |    1 |    1 |
    |  2 | jia  |    1 |    1 |
    |  3 | jia  |    1 |    0 |
    +----+------+------+------+
    3 rows in set (0.00 sec)mysql>