query1.close; query1.sql.text:='select distinct 姓名 from table_name';//过滤掉重复的 try query1.open; except exit; end; if query1.isempty then exit;//没记录退出 query1.first; while not query1.eof do begin listbox1.items.add(query1.fieldbyname('name').asstring); query1.next; end;
if (combobox1.Items.Count)<>(ADOTable1.RecordCount) then begin ADOTable1.First; while(not ADOTable1.eof) do begin combobox1.items.add(ADOTable1.fieldbyname('姓名').asstring); ADOTable1.next; end; end;
比如:
if combobox1的行數 <> fieldbyname(姓名) then
combobox1.items.add((query1.sql.fieldbyname(姓名);)
else
...........//寫你的代碼,不寫也行
DISTINCT 关键字可从 SELECT 语句的结果中除去重复的行。如果没有指定 DISTINCT,那么将返回所有行,包括重复的行。例如,如果在 titleauthor 中选择所有作者 ID 时未使用 DISTINCT,那么将会返回下列行(其中包括一些重复的行):USE pubs
SELECT au_id
FROM titleauthor下面是结果集:au_id
-----------
172-32-1176
213-46-8915
213-46-8915
238-95-7766
267-41-2394
267-41-2394
274-80-9391
409-56-7008
427-17-2319
472-27-2349
486-29-1786
486-29-1786
648-92-1872
672-71-3249
712-45-1867
722-51-5454
724-80-9391
724-80-9391
756-30-7391
807-91-6654
846-92-7186
899-46-2035
899-46-2035
998-72-3567
998-72-3567 (25 row(s) affected)而使用了 DISTINCT 后,就能够除去重复项,而只查看唯一的作者 ID:USE pubs
SELECT DISTINCT au_id
FROM titleauthor下面是结果集:au_id
-----------
172-32-1176
213-46-8915
238-95-7766
267-41-2394
274-80-9391
409-56-7008
427-17-2319
472-27-2349
486-29-1786
648-92-1872
672-71-3249
712-45-1867
722-51-5454
724-80-9391
756-30-7391
807-91-6654
846-92-7186
899-46-2035
998-72-3567 (19 row(s) affected)重要 涉及 DISTINCT 的语句的输出取决于列的排序规则或应用 DISTINCT 的表达式。有关不同排序规则的效果的更多信息,请参见 SQL Server 排序规则基础知识。
对于 DISTINCT 关键字来说,各空值将被认为是相互重复的内容。当 SELECT 语句中包括 DISTINCT 时,不论遇到多少个空值,在结果中只返回一个 NULL。说明 为了与 SQL-92 标准和其它的 Microsoft® SQL Server™ 版本兼容,ALL 关键字可以显式请求所有行。但是,由于 ALL 是默认的,所以无需指定它。
©1988-2000 Microsoft Corporation。保留所有权利。
begin
comboyjlm.items.Add(fields[2].asstring);
end;
jackie168(星星知我心) ,我没有明白你的意思,可以详细一点吗??
query1.sql.text:='select distinct 姓名 from table_name';//过滤掉重复的
try
query1.open;
except
exit;
end;
if query1.isempty then
exit;//没记录退出
query1.first;
while not query1.eof do
begin
listbox1.items.add(query1.fieldbyname('name').asstring);
query1.next;
end;
begin
ADOTable1.First;
while(not ADOTable1.eof) do
begin
combobox1.items.add(ADOTable1.fieldbyname('姓名').asstring);
ADOTable1.next;
end;
end;
我上面的代碼不能實現你要的效果嗎?
用sql中的distinct
2.查询出结果,在添加的时候,进行判断,若重复,不添加