id     attr    id_choosed     attr_count  
1       a       1             0
2       a       1             0
3       a       0             0
4       b       1             0
5       b       1             0 
当满足某条件后,id被选中,更新id_choosed 和attr_count
如下:
更新后
id      attr   id_choosed     attr_count
1       a       1             2
2       a       1             2
3       a       0             0
4       b       1             2
5       b       1             2 
这样的update语句怎么写?

解决方案 »

  1.   

    attr:是该id所属的类别
    id_choosed:1为该id被选中,0为未被选中
    attr_count:相同类别的被选中的个数这样的sql怎么写?
      

  2.   

    update 表名 set id_choosed=某条件,attr_count=某条件 where id=条件;
      

  3.   

    我说的某条件只是更新id_choosed,不影响attr_count的
      

  4.   

    多写几个sql吧,不要一个sql就写完了。Update TableName set attr_count=
      (select count(*) from tableName where attr='a')
      

  5.   

    To hujinger:
     数据量很大的,不能这样写
      

  6.   

    我想楼主的思路是不是搞清楚了
    你应该是要在程序里面实现:
    选中了N条记录,然后要将选中的记录的id_choosed,attr_count更新
    是吧?
      

  7.   

    "选中了N条记录,然后要将选中的记录的id_choosed,attr_count更新"
    是的啊,
      

  8.   

    那你在程序里面将选中的记录存入一个字符串数组里面(TStrings)
    然后用个循环不就把所选中的记录更新了啊
    var
      sL: TStrings;
      I: Integer;
    begin 
      sl:= TstringList.create;
    {  //加入选中的ID
      sL.add();}
      for I := 0 to sL.count-1 do
    begin
      with adoQuery do
      begin
        close;
        sql.text:= 'update TableName set id_choosed = 1, attr_count={你要更新的值}    where id = ' + QuotedStr(sl.string[i]);
        execsql; 
      end;
    end;
      

  9.   

    我原来是这样写的:
    update tablename A,TT set A.attr_count=A.attr_count+1 
    where A.id=TT.id and A.attr_count<2
    其中TT是临时表,是所有id_Choosed=1的临时表问题是这样更新后,tablename表的attr_count都是1,