delete from AAA as a
where not exists(select 1 from AAA where No=a.No and  LC=a. LC and ID<a.id)

解决方案 »

  1.   

    方法2:
    delete from AAA as a
    where id not in (select min(id) from AAA where No=a.No and  LC=a. LC )
      

  2.   

    楼主可以查询要删除结果
    select * from AAA as a
    where not exists(select 1 from AAA where No=a.No and  LC=a. LC and ID<a.id)
    select * from AAA as a
    where id not in (select min(id) from AAA where No=a.No and  LC=a.LC )
      

  3.   

    select * from AAA as a--把表名AAA转换为别名a
    where not exists(select 1 from AAA where No=a.No and  LC=a.LC and ID<a.id)
    --a. LC这里多了一个空格去掉delete a--加上别名
    from AAA as a
    where not exists(select 1 from AAA where No=a.No and  LC=a.LC and ID<a.id)
    delete a--加上别名
    from AAA as a
    where id not in (select min(id) from AAA where No=a.No and  LC=a. LC )
      

  4.   

    不行呀!以上语句执行时均提示:在关键字 'As' 附近有语法错误。我想是“Delete From AAA As A行不通的原因。
      

  5.   

    加上别名
    delete a--加上别名
    from AAA as a
    where not exists(select 1 from AAA where No=a.No and  LC=a.LC and ID<a.id)
    delete a--加上别名
    from AAA as a
    where id not in (select min(id) from AAA where No=a.No and  LC=a.LC )
      

  6.   

    select id,nod,lc from aaa where id in (select  min(id) as id from AAA group by nod,lc) order by id
      

  7.   

    修正后的可以了,网上查询到的均是用几条命令,但“roy_88(中国风_燃烧你的激情!!!) ”同志你的SQL命令够精减。非常感谢!
      

  8.   

    “yqwaxyq()”同志的汇总命令也不错,值得学习。方法也多样化了,删除也行,筛选也行。
      

  9.   

    create table ta (ID int  ,   [No] varchar(2)    ,   LC smallint)
    insert ta select 1,                            'A',                1
    union all select 2,                            'A',                2
    union all select 3,                            'B',                1
    union all select 4,                            'B',                2
    union all select 5,                            'B',                2 --drop table tadelete a--加上别名
    from ta as a
    where  exists(select 1 from ta where No=a.No and  LC=a.LC and ID<a.id)select * from ta
    ID          No   LC     
    ----------- ---- ------ 
    1           A    1
    2           A    2
    3           B    1
    4           B    2(所影响的行数为 4 行)
      

  10.   

    上面把not exists 改为exists就行了
    create table ta (ID int  ,   [No] varchar(2)    ,   LC smallint)
    insert ta select 1,                            'A',                1
    union all select 2,                            'A',                2
    union all select 3,                            'B',                1
    union all select 4,                            'B',                2
    union all select 5,                            'B',                2 --drop table tadelete a--加上别名
    from ta as a
    where id not in (select min(id) from ta where No=a.No and  LC=a.LC )select * from ta
    ID          No   LC     
    ----------- ---- ------ 
    1           A    1
    2           A    2
    3           B    1
    4           B    2(所影响的行数为 4 行)
      

  11.   

    create table AAA(ID int, No varchar(10), LC smallint)
    insert AAA select 1,                            'A',                1
    union all select 2,                            'A',                2
    union all select 3,                            'B',                1
    union all select 4,                            'B',                2
    union all select 5,                            'B',                2delete AAA
    where ID not in(
    select min(ID) from AAA group by NO, LC
    )select * from AAA--result
    ID          No         LC     
    ----------- ---------- ------ 
    1           A          1
    2           A          2
    3           B          1
    4           B          2(4 row(s) affected)
      

  12.   

    TO:“marco08(天道酬勤)”同志的分组删除方法有创意,和之前二位的不同。