多条件查询问题

老杨_sz 2009-08-27 11:37:48
edit1,edit2,edit3,edit4

使用如下语句行不通:
var str1,str2,str3:string;
begin
if Edit1.Text='' then
str1:=''
else
str1:=' and FieldName='''+Edit1.Text+'''';
if Edit2.Text='' then
str2:=''
else
str2:=' and FieldName='''+Edit2.Text+'''';
if Edit3.Text='' then
str3:=''
else
str3:=' and FieldName='''+Edit3.Text+'''';
ADOQuery.Sql.Text:='select * from TableName where 1=1'+str1+str2+str3;
使用如下语句也行不通:
procedure TForm5.Button1Click(Sender: TObject);
Var
Tkind,Tcbs,Tname,Twriter,e1,e2,e3,e4:string;
i,j,k:integer;
temp:boolean;
begin
e1:=edit1.text;
e2:=edit2.text;
e3:=edit3.Text;
e4:=edit4.text;
temp:=false;
if(e1<>'')then
begin
if(temp=false)then
Tkind:=' and mainbook.kind="'+e1 + '"' ;
temp:=true;
end
else Tkind:=' ';
if(e2<>'')then
begin
if(temp=false)then
Twriter:=' and mainbook.ze="'+e2 + '"'
else
Twriter:=' and mainbook.ze="'+e2 +'"';
temp:=true;
end
else Twriter:=' ';
if(e3<>'')then
begin
if(temp=false)then
Tname:=' and mainbook.name="'+e3 +'"'
else
Tname:=' and mainbook.name="'+e3+'"';
temp:=true;
end
else Tname:=' ';

if(e4<>'')then
begin
if(temp=false)then
Tcbs:=' and mainbook.cbs="'+e4+'"'
else
Tcbs:=' and mainbook.cbs="'+e4+'"';
temp:=true;
end
else Tcbs:=' ';
Query1.close;
Query1.SQL.clear;
Query1.SQL.Add('Select * ');
Query1.SQL.Add('From mainbook.db ');

Query1.SQL.Add('where 1=1') ;

Query1.SQL.Add(Tkind+twriter+Tcbs+Tname);


Query1.Open ;


要求:如果edit1为空,不查询此字段,依此类推;四个edit全为空,查询整张表,只要其中有一个为空就不查询此字段。
...全文
62 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wh_wangjun 2009-08-27
  • 打赏
  • 举报
回复
var str:string;
begin
str := '';
if edit1.text<>'' then str := ' fieldName='+#39+edit1.text+#39;
if edit2.text<>'' then begin
if str<>'' then str := str+' and ';
str := str +' fieldName='+#39+edit2.text+#39;
end;
if edit3.text<>'' then begin
if str<>'' then str := str+' and ';
str := str +' fieldName='+#39+edit3.text+#39;
end;
if str<>'' then str := ' where '+str;
ADOQuery.Sql.Text:='select * from TableName '+str;
end;
老杨_sz 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ameken 的回复:]
Delphi(Pascal) codevar str:string;begin
str :='1=1';if Edit1.Text<>''then
str:=str+' and FieldName='''+Edit1.Text+'''';if Edit2.Text<>''then
str:=str+' and FieldName='''+Edit2.Text+'''';if E¡­
[/Quote]

如果edit1.text=''呢?
Dision LI 2009-08-27
  • 打赏
  • 举报
回复

var str:string;
begin
str :='1=1';
if Edit1.Text<>'' then
str:=str+' and FieldName='''+Edit1.Text+'''';
if Edit2.Text<>'' then
str:=str+' and FieldName='''+Edit2.Text+'''';
if Edit3.Text<>'' then
str:=str+' and FieldName='''+Edit3.Text+'''';
ADOQuery.Sql.Text:='select * from TableName where'+str;
bdmh 2009-08-27
  • 打赏
  • 举报
回复
把三个条件用一个字符串来组合,那样判断该字符串是否为空,如果为空,就去掉where
taste品味 2009-08-27
  • 打赏
  • 举报
回复
begin
if Edit1.Text<>'' then
str:=str+' and FieldName='''+Edit1.Text+'''';
if Edit2.Text<>'' then
str:=str+' and FieldName='''+Edit2.Text+'''';
if Edit3.Text<>'' then
str:=str+' and FieldName='''+Edit3.Text+'''';
ADOQuery.Sql.Text:='select * from TableName where 1=1 '+str;
end;

2,496

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧