有一表,表结构如下:
id    name 
1     aaa
2     bb
3     aaa
4     cc
怎样用一条SQL语句,任意删除一条name是aaa的记录(只能删除一条)

解决方案 »

  1.   

    在数据库中要删除数据就用
    DELETE FROM<表名> [WHERE<删除条件>] 
    WHERE 是注明要删除哪一行,如果不注明条件就会全部删掉了
    还有用DELETE 不能删除某一列,要删就是删一行或多行
      

  2.   

    delete from table where id =(select top 1 id from table where name='aaa')
      

  3.   

    joejoe1991() ( ) 信誉:98  2007-08-11 15:20:43  得分: 0  
     
     
       delete from table where id =(select top 1 id from table where name='aaa')
      
     
    是top 1还是first 1呢?哪个对?
      

  4.   

    mssql里是top 1 别的数据库我不知道
      

  5.   

    delete from table where id =(select top 1 id from table where name='aaa')select top 1 的意思是指从符合条件的记录中选取第一个,不是只选取一个,一般而言这个后面要跟order by来确定你是根据什么来对你选出来的这些记录进行排序的,否则删肯定是删一个,但是删哪个不同的数据库会有不同的解释,这样容易出问题的.不如写成delete from table where id =(select top 1 id from table where name='aaa' order by id)这样能确保删除的是id号小的那条记录
      

  6.   

    谢啦 如果是oracle还是用top吗?
      

  7.   

    是,这个属于SQL语言范畴,和数据库类型无关,不管是那种数据库,只要是支持SQL语句的,都可以使用top关键词
      

  8.   

    MYSQL:
    delete from table where id =(select id from table where name='aaa' limit 1)
    测试下。
      

  9.   

    oracle数据库不支持top关键字,得用rownum来控制
    例如:
    取出前一条
    select * from table where rownum<=1
    取出前两条
    select * from table where rownum<=2
      

  10.   

    那是否楼主的问题在ORACLE中就可以写成
    select * from table where id=(select * from table where name='aaa' and rownum<=1)呢?
      

  11.   

    mysql应该是:
    delete from table_name where field_name='aaa' limit 1;oracle:(不是很确定)
    delete from table_name 
    where id = (select id from (select t.id from table_name t where t.field_name='aaa') where  rownum = 1);
      

  12.   

    Mysql 不支持top的,好像目前就mssql支持吧。
    mysql应该limit
      

  13.   

    MySQL:delete from table_name where field_name ='aaa' limit 1;