这里有一列表!值如下:
1
1
2
3
4
4
4
5
5
5
5
这里要删除重复的项!只保留最后一个!(这里有两个1,保留后一个!三个4,保留最后一个),该怎么写?

解决方案 »

  1.   

    给你写了个函数,我这里没有装Delphi,你自己调试看看吧:
    //输入一个String类型的值
    //返回值为String类型(即:你想要的结果)
    function GetUniqueList(Const strs:String):string
    var
       uniqueChar:string;
       tmpstr:String;
       thepos:integer;
       thelen:integer;
    begin
    //如果字符长度小于2,则直接返回输入值。
    if length(strs)<2 then
      tmpstr:=strs;thelen=length(strs);
    uniqueChar:=copy(strs,1,1);
    for thepos=1 to thelen-1 do
    begin
      //如果字符已经发生变化,则将其值保存下来
      if copy(strs,thepos+1,1)<>uniquechar then
        tmpstr:=tmpstr+uniqueChar
      else
          if thepos=thelen-1 then
             tmpstr:=tmpstr+uniqueChar;
      //取出下一个字符
      uniquechar:=copy(strs,thepos+1,1);
    end;
    result:=tmpstr;
    end;
      

  2.   

    Alter Table TabA add Add_ID int identity(1,1)
    goDelete TabA 
      from TabA a, (select ColA,Max(Add_ID) as Add_ID from TabA group by ColA) b
    where a.ColA=b.ColA and a.Add_ID<>b.Add_IDAlter table TabA drop column Add_ID
    go
      

  3.   

    Bes96261(秋水孤鶩) 的方法好。
    在SQL表中,因为没有记录号及当前记录的概念,如通过表循环来检查重复记录的话,最后一条记录找到了,但前面记录删除比较麻烦(如不更改表结构),当然变通的做法是可以把最后一条记录抓出来。
    数据如下(可放在文本文件中,以文本文件做数据库来测试):
    id1,id2
    1,a
    1,b
    2,c
    2,d
    2,e
    3,f
    3,g
    3,h
    4,i
    4,j
    4,k代码如下
    procedure TForm1.Button7Click(Sender: TObject);
    var
      s1,s2:string;//s1,s2用来保存当前记录和下记录id1的值;
    begin
      with adotable1 do
      begin
      Active;
      First;
      while not Eof do
      begin
      s1:=fieldbyname('id1').AsString;
      Next;
      s2:=fieldbyname('id1').AsString;
      if s1<>s2 then
       begin
         MoveBy(-1);//注意这里指针移动。
         listbox1.Items.Add(fieldbyname('id1').AsString+','+fieldbyname('id2').AsString);
         MoveBy(1);
        end;
      end;
      end;
    end;