delete from 你的表名
where 你的表名.otime <> (select max(otime) from 你的表名  where id = 你的表名.id)试试!

解决方案 »

  1.   

    delete from 你的表名
    where exists(select * from 你的表名 a where a.id=你的表名.id and a.otime>你的表名.otime)
      

  2.   


    语法是一样的,不过,表名不能是变量。exec ( 'delete from ' + @表名变量 + ' a where a.otime not in (select max(b.otime) from ' + @表名变量 + ' b group by b.id)' )
    '
      

  3.   

    delete from table where table.otime not in (select max(otime)  from table group by id)这样一定行的你试试吧
      

  4.   

    这是行,不过我的表在远端服务器上,如果连接上后where table.otime 就会是where "远端服务器名"."数据库名"."表名".otime,前缀太多,我本来想用别名a,然后用a.otime,但是delete from 你的表名 a 不行,应该怎么办呢?
      

  5.   

    还有在update里好像也不行,是不是?
      

  6.   

    我的一个贴子http://community.csdn.net/Expert/topic/3210/3210136.xml?temp=.9744074
    也有类似的问题,ghosthjt(天煞孤星)写的从远端数据库更新本地的可以,如果用本地数据去更新远端的表就有问题,请各位看看有没有什么好办法?
      

  7.   

    delete from 你的表名
    where exists(select * from 你的表名 a where a.id=你的表名.id and a.otime>你的表名.otime)
    --delete from 后的表名不能用别名的,但where里涉及的表名可以用别名
      

  8.   

    to: victorycyz(中海)
    我没有太明白你写的,是不是:
    exec ( 'delete from ' +表名 + ' a where a.OTIME not in (select max(b.OTIME) from ' + 表名 + ' b group by b.id)' ) 
    怎么不行呢?
    to:pbsql(风云) 
    如果是远程数据库的表该怎么办呢?
    比如你的表名=[服务器名].[数据库名].[表名],应该怎么写?谢谢各位!
      

  9.   

    [服务器名].[数据库名].[dbo].[表名]
    or
    [服务器名].[数据库名]..[表名]
      

  10.   

    可是在你的语句里
    delete from 你的表名
    where exists(select * from 你的表名 a where a.id=你的表名.id and a.otime>你的表名.otime)
    如果是远程的表[服务器名].[数据库名]..[表名]  改后成了:
    delete from [服务器名].[数据库名]..[表名]
    where exists(select * from [服务器名].[数据库名]..[表名] a where a.id名 =[服务器名].[数据库名]..[表名].id and a.otime>[服务器名].[数据库名]..[表名].otime)
    这样[服务器名].[数据库名]..[表名].id 提示前缀太多。
      

  11.   

    select *
    from (select *
    from bfhftbal a
    where not exists(select 1 from 你的表 b where a.ID=b.ID and a.otime<b.otime) a
      

  12.   

    上一句有误!
    select *
    from 你的表 a
    where not exists(select 1 from 你的表 b where a.ID=b.ID and a.otime<b.otime)
      

  13.   

    MS SQL可以使用表的转义名:Select a.id,b.otime from (select * from tableAname where a.id=b.id) as a  tableBname as b
    试试!!