我有一个包含 7000 多条记录的 MySQL 数据库。在几条记录中的一个字段 (link_to_pdf) 中,我有重复的字段:Fe
A#13#10B#13#10C#13#10A#13#10乙
每个字段条目以 CR/LF 分隔。(这里是 #13#10)
如何删除 MySQL 过程中字段中的重复项?(上例中的粗体)
非常感谢 Walter
答案1
procedure TForm1.Button28Click(Sender: TObject);
var
pdfs: string;
begin
UniConnection1.Loginprompt:=true;
UniScript_cr_linknames.Execute;
uniquery8.Execute;
uniquery9.sql.text:='Select * from refs order by id asc;';
uniquery9.execute;
while not(uniquery9.eof) do
begin
uniquery11.sql.text:='Select * from linknames where id = :vonid group by linkname order by linkname; ';
uniquery11.parambyname('vonid').asinteger:=uniquery9.fieldbyname('id').asinteger;
uniquery11.execute;
pdfs:='';
while not(uniquery11.eof) do
begin
if pdfs='' then
pdfs:=uniquery11.fieldbyname('linkname').asstring
else
pdfs:=pdfs+#13#10+ uniquery11.fieldbyname('linkname').asstring;
//if uniquery11.RecordCount > 1 then ShowMessage(format('Update refs set link_to_pdf = %s where id=%d;',[pdfs,uniquery9.fieldbyname('id').asinteger]));
uniquery11.Next;
end;
uniquery10.sql.text:='Update refs set link_to_pdf = :links where id=:vonid;';
uniquery10.parambyname('links').asstring:=pdfs;
uniquery10.parambyname('vonid').asinteger:=uniquery9.fieldbyname('id').asinteger;
uniquery10.execute;
if Pos(#13,pdfs)> 0 then WriteLOgMemo(Format('UPD: Id: %d done with more than one unique-LINK ) :',[uniquery9.fieldbyname('id').asinteger]));
uniquery9.next;
end;
end;
Query8 和脚本如下所示:
insert into linknames (id,linkname)
(SELECT
refs.id,
SUBSTRING_INDEX(SUBSTRING_INDEX(refs.link_to_pdf, '\n', numbers.n), '\n', -1) link_to_pdf
FROM
numbers INNER JOIN refs
ON CHAR_LENGTH(refs.link_to_pdf)
-CHAR_LENGTH(REPLACE(refs.link_to_pdf, '\n', ''))>=numbers.n-1
ORDER BY
id, n)
数字表只是一张带有数字的表格:
CREATE TABLE `numbers` (
`n` varchar(255) DEFAULT NULL,
`recnum` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`recnum`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;