数据表user中有id,uid,name三个字段
用一条SQL语句实现以下功能:
(1),请求删除数据库中uid=2,name="wn"的一条记录
(2),当数据表中uid=2的记录少于2条时不得删除,uid=2的记录数等于或大于2条时删除(1)所述记录

解决方案 »

  1.   

    贴建表及插入记录的SQL,及要求结果出来看看
    delete a from tt a inner join
    (select uid from tt where uid=2 group by uid having count(*)>=2) b
    on a.uid=b.uid and a.name='wn'
      

  2.   

    delete a
    from user a,(select count(*) as cnt from user where uid=2)b
    where a.uid=2 and a.name='wn' and b.cnt>=2
      

  3.   


    create table temp(id,uid,name)insert into temp
    select *
    from user u1
    where uid=2 and name='wn' and not exists (select 1 from user where uid=2 and name='wn' and u1.id>id)delete from user where uid=2 and name='wn'
    insert into user
    select *
    from temp
      

  4.   

    delete a from tt a inner join
    (select uid from tt where uid=2 group by uid having count(*)>=2) b
    on a.uid=b.uid and a.name='wn'和:
    delete a
    from user a,(select count(*) as cnt from user where uid=2)b
    where a.uid=2 and a.name='wn' and b.cnt>=2刚才我试了下,都可以。rucypli的方法没试,应该也可以就是麻烦了点。