在mysql区发了帖,没人回,求一个sql语句,
就是我要删除某个人某类记录第十条以外的记录,
表结构简单表示为: id(主键,自增), int 
                 type          int
                 info          string
                 atime          datetime。
我试了delete from table where id = 1 and type = 1 order by atime desc limit 10,-1;
但mysql说语法错误。试了
delete from table where id not in (select id from table where type = 1 and id = 1 order by atime desc limit 10)
但mysql不支持limit在子查询中的应用。又试了    
delete from table where id < (select min(id) as id from table where  type = 7 and id = 1 order by time desc limit 10)
但mysql还不支持此语句。
因此请教大家,给个答案吧,头痛半天了。

解决方案 »

  1.   

    SQL好久没用了 忘的差不多了  等过段时间捡起来看看吧 
    你的头像很美...
      

  2.   

    delete from tableName where id not in(select top(10) id from tableName)
      

  3.   

    mysql 里没有top这个函数啊!
      

  4.   

    mysql 不支持top语句,大哥
      

  5.   

    美女~~我没用过mysql
    平时一直在用mssql和Oracle
    mssql中是这样
    delete [table] where id not exists(select top(10) id from [table])
    不过我想mysql中应该是。你可以试试
    delete [from](mssql可以省去) [table] where id not in(select id from [table] limit 0,10)
    我也是学子,多多指教!
      

  6.   

    不是吧?没用过mysql。你怎么用这种啊?
      

  7.   

    SQL的知道在这里发不会被删吗?
      

  8.   

    delete [from](mssql可以省去) [table] where id not in(select id from [table] limit 0,10) mysql中对于删除语句中查询部分和删除部分不能是一个表
      

  9.   

    楼主的要求,mysql里好像一句sql搞不定吧
      

  10.   


    delete from tableName where id not in(select top(10) id from tableName) 
      

  11.   

     delete * from tableName where id not in(select top(10) from tableName)
      

  12.   

    这么多人竟然没有一个懂SQL?
    id是自增,select id from table where type = 1 and id = 1 order by atime desc limit 10
    这句话就看不懂了,如果where中id = 1.那还要查id干什么?另外mysql不支持in(not in)的子查询中有limit,这种问题也解决不了?delete from table where id not in 
      (select id from table where 条件自己弄清楚 order by atime limit) ;不支持.
    自己包一层就完全可以解决,太容易的问题.
    delete from table where id not in 
      (      select id from 
                   (select id from table where 条件自己弄清楚 order by atime limit) b
       ) ;
    OK,把里面的limit子查询再做一张虚表,对外层来说没有limit子查询了,骗外层一下就行了.
      

  13.   

    写出delete from table where id = 1 and type = 1 order by atime desc limit 10,-1; 
    这样的语句还能拿到1000块钱,老板没少给.
      

  14.   

    create view my_view as select * from table where type = 1 and id = 1 order by atime desc limit 10;
    delete from table where id not in (select id from my_view);
      

  15.   


    昏,我说了是简化表,语句 where id = 1中的id实际是一个人物id,和主键自增id不一样,
    怪我在写时没标清。
      

  16.   


    说了你的语句不行,mysql不支持delete语句中查询部分和删除部分不能是一个表。
    我发帖的最后一个语句已经做了。
    自己没搞清就不要老批评别人。不实践就没有发言权!
      

  17.   

    不知道 你是用JDBC来做 还是用Hibernate 还是Ibatis来做 数据库底层操作
      

  18.   

    delete from table where id not in 
      (select id from table where 条件自己弄清楚 order by atime limit) 
    这种语句会报:
    “you can't specify target table (table名) for update in from clause";
      

  19.   

    可以使用存储过程来写;delete from table where id < (select min(id) as id from table where  type = 7 and id = 1 order by time desc limit 10) 
    declare @num int;
    begin
      @num = select min(id) as id from table where  type = 7 and id = 1 order by time desc limit 10;
      
      if(@num >0 )
      delete from table where id < @num;
      commit;  
      end if;
    end;
      
      

  20.   

    用jdbc来做的。
    项目要求速度第一
      

  21.   

    你简单在胡说:
    delete from table where id not in 
      (      select id from 
                  (select id from table where 条件自己弄清楚 order by atime limit) b 
      ) ; 
    绝对可是,除非你用的是火星版的mysql,因为是不是可以不是你说不可以就不可以的,其他人都可以验证.
      

  22.   

     create table test(
     id int(4) not null auto_increment primary key,
     type int(4),
     info varchar(20),
     atime datetime
     )插入测试记录
    insert into test (type,info,atime) values (10,'test1',now());
    insert into test (type,info,atime) values (10,'test2',now());
    insert into test (type,info,atime) values (10,'test3',now());
    insert into test (type,info,atime) values (10,'test4',now());
    insert into test (type,info,atime) values (10,'test5',now());
    insert into test (type,info,atime) values (10,'test6',now());
    insert into test (type,info,atime) values (10,'test7',now());我们现在要保留type=10的五条记录,大于5条的删除. delete from test where id not in (select id from test where type = 10 order by atime limit 5);
    ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'换包装语句:delete from test where id not in (
    select id from (
    select id from test where type = 10 order by atime limit 5   
    ) b
    ) and type = 10;Query OK, 2 rows affected (0.08 sec)成功删除了type = 10的多于5条记录的数据.mysql> select * from test where type = 10;
    +----+------+-------+---------------------+
    | id | type | info  | atime               |
    +----+------+-------+---------------------+
    | 15 |   10 | test1 | 2008-09-22 10:27:28 |
    | 16 |   10 | test2 | 2008-09-22 10:27:28 |
    | 17 |   10 | test3 | 2008-09-22 10:27:28 |
    | 18 |   10 | test4 | 2008-09-22 10:27:28 |
    | 19 |   10 | test5 | 2008-09-22 10:27:28 |
    +----+------+-------+---------------------+
    5 rows in set (0.00 sec)结果完全正确.
      

  23.   

    delete from table where id = 1 and type = 1 order by atime desc limit 10,-1; 
    这个语句并不是说条件对不对,最主要的是在delete语句中直接order by还limit,你连删除和查询都没有弄清楚.
      

  24.   

    DELETE abc FROM abc
      INNER JOIN (
          SELECT a.id
          FROM abc a
          WHERE (
              SELECT count(id) 
                FROM abc b 
                WHERE b.id <= a.id 
                  AND b.type = '1'   -- 查询条件
            ) > 10
          AND a.type = '1'  -- 查询条件
        ) tmp
      ON abc.id = tmp.id;