IF EDIT1。TXT<>‘ ’THEN SQL.add('AND... '); IF EDIT2。TXT<>‘ ’THEN SQL.add('AND... ');
if 条件1 then sql:=sql+条件1; if sql<>'' then sql:=sql+'and'+条件2 sql:=sql+条件2 。
只有四个条件还不简单?if……then……else
用if判断,除了第一个条件外都要加And,因此你的第一个条件有没有and要分析一下!
if... then... else if 条件1 then sql.add('and ...') if 条件2then sql.add('and ...') if 条件3then sql.add('and ...') if 条件41 then sql.add('and ...')
f:bool; f:=false; if 条件1 or 条件2 or 条件3 or 条件4 then sql:= sql+ 'where'; else return sql; if 条件1 then begin sql:=sql+条件1; f:= true; end if 条件2 then begin if f then sql:=sql+' and ' + 条件2; else sql:=sql+条件2; f:= true; endif 条件3 then begin if f then sql:=sql+' and ' + 条件3; else sql:=sql+条件3; f:= true; end if 条件4 then begin if f then sql:=sql+' and ' + 条件4; else sql:=sql+条件4; f:= true; end return sql
begin adoquery1.close; adoquery1.sql.clear; adoquery1.sql.add('select * from 教师表'); if edit1.Text<> then adoquery1.SQL.Add('where 教师编号:='''+edit1.text''''); if edit2.Text<> then adoquery1.SQL.Add('and 教师姓名:='''edit2.text''''); if combobox1.Text<> then adoquery1.SQL.Add('and 所属院系:='''combobox1.text'); if combobox2.Text<> then adoquery1.SQL.Add('and 任教科目:='''combobox2.text''''); adoquery1.open; end; 这样写可以么?
用TCheckBox做选择, 用if ... then ...做判断就可
wherestr:=' where 1 = 1'; if 条件1 <> '' then wherestr:= wherestr+' and .....'; if 条件2 <> '' then wherestr:= wherestr+' and .....'; .......
//如果选择日期,加入日期条件 if cbbdate.Checked =true then StrSql:=StrSql+'and sddate between '+'#'+datetostr(dtpsfdate.Date)+ '#'+' and '+'#'+datetostr(dtpsfend.Date)+'#'; //如果选择客户 if cbkh1.Checked=true then begin adofind.SQL.Clear; adofind.SQL.Add('select * from khxx where khmc='''+cbkh.Text+''''); adofind.Open; StrSql:=StrSql+'and khdm='''+adofind.fieldbyname('khdm').AsString+''''; end; //如果选择表名 if cbbm1.Checked=true then begin adofind.SQL.Clear; adofind.SQL.Add('select * from khbh where khbm='''+cbbm.Text+''''); adofind.Open; StrSql:=StrSql+'and khbh='''+adofind.fieldbyname('khbh').AsString+''''; end; //如果选择收费员 if cbsfy1.Checked=true then begin adofind.SQL.Clear; adofind.SQL.Add('select * from czyb where czyxm='''+cbsfy.Text+''''); adofind.Open; StrSql:=StrSql+'and ysf='''+adofind.fieldbyname('czybm').AsString+''''; end; //如果选择开票员 if cbkpy1.Checked=true then begin adofind.SQL.Clear; adofind.SQL.Add('select * from czyb where czyxm='''+cbkpy.Text+''''); adofind.Open; StrSql:=StrSql+'and ykp='''+adofind.fieldbyname('czybm').AsString+''''; end; //如果是选择水费或者电费 if rbsf.Checked=true then begin StrSql:=StrSql+' and khbh like '''+'w%'+''''; end else begin StrSql:=StrSql+' and khbh like '''+'e%'+''''; end; //开始查找数据 StrSql:=copy(StrSql,5,255); if StrSql='' then begin showmessage('请选择一个查询条件,在其前面打钩即可'); exit; end; with dmmain.ADOQKhsf do begin sql.Clear; sql.Add('select * from khsf where'); sql.Add(StrSql); open; end;
SQL.add('... ');
SQL.add('AND... ');
SQL.add('AND...');
...
ExecSQL;
IF EDIT2。TXT<>‘ ’THEN SQL.add('AND... ');
sql:=sql+条件1;
if sql<>'' then
sql:=sql+'and'+条件2
sql:=sql+条件2
。
if 条件1 then sql.add('and ...')
if 条件2then sql.add('and ...')
if 条件3then sql.add('and ...')
if 条件41 then sql.add('and ...')
f:=false;
if 条件1 or 条件2 or 条件3 or 条件4 then
sql:= sql+ 'where';
else return sql;
if 条件1 then
begin
sql:=sql+条件1;
f:= true;
end
if 条件2 then
begin
if f then
sql:=sql+' and ' + 条件2;
else
sql:=sql+条件2;
f:= true;
endif 条件3 then
begin
if f then
sql:=sql+' and ' + 条件3;
else
sql:=sql+条件3;
f:= true;
end
if 条件4 then
begin
if f then
sql:=sql+' and ' + 条件4;
else
sql:=sql+条件4;
f:= true;
end
return sql
adoquery1.close;
adoquery1.sql.clear;
adoquery1.sql.add('select * from 教师表');
if edit1.Text<> then
adoquery1.SQL.Add('where 教师编号:='''+edit1.text'''');
if edit2.Text<> then
adoquery1.SQL.Add('and 教师姓名:='''edit2.text'''');
if combobox1.Text<> then
adoquery1.SQL.Add('and 所属院系:='''combobox1.text');
if combobox2.Text<> then
adoquery1.SQL.Add('and 任教科目:='''combobox2.text''''); adoquery1.open;
end;
这样写可以么?
用if ... then ...做判断就可
if 条件1 <> '' then
wherestr:= wherestr+' and .....';
if 条件2 <> '' then
wherestr:= wherestr+' and .....';
.......
if cbbdate.Checked =true then
StrSql:=StrSql+'and sddate between '+'#'+datetostr(dtpsfdate.Date)+
'#'+' and '+'#'+datetostr(dtpsfend.Date)+'#';
//如果选择客户
if cbkh1.Checked=true then
begin
adofind.SQL.Clear;
adofind.SQL.Add('select * from khxx where khmc='''+cbkh.Text+'''');
adofind.Open;
StrSql:=StrSql+'and khdm='''+adofind.fieldbyname('khdm').AsString+'''';
end;
//如果选择表名
if cbbm1.Checked=true then
begin
adofind.SQL.Clear;
adofind.SQL.Add('select * from khbh where khbm='''+cbbm.Text+'''');
adofind.Open;
StrSql:=StrSql+'and khbh='''+adofind.fieldbyname('khbh').AsString+'''';
end;
//如果选择收费员
if cbsfy1.Checked=true then
begin
adofind.SQL.Clear;
adofind.SQL.Add('select * from czyb where czyxm='''+cbsfy.Text+'''');
adofind.Open;
StrSql:=StrSql+'and ysf='''+adofind.fieldbyname('czybm').AsString+'''';
end;
//如果选择开票员
if cbkpy1.Checked=true then
begin
adofind.SQL.Clear;
adofind.SQL.Add('select * from czyb where czyxm='''+cbkpy.Text+'''');
adofind.Open;
StrSql:=StrSql+'and ykp='''+adofind.fieldbyname('czybm').AsString+'''';
end;
//如果是选择水费或者电费
if rbsf.Checked=true then
begin
StrSql:=StrSql+' and khbh like '''+'w%'+'''';
end
else
begin
StrSql:=StrSql+' and khbh like '''+'e%'+'''';
end;
//开始查找数据
StrSql:=copy(StrSql,5,255);
if StrSql='' then
begin
showmessage('请选择一个查询条件,在其前面打钩即可');
exit;
end;
with dmmain.ADOQKhsf do
begin
sql.Clear;
sql.Add('select * from khsf where');
sql.Add(StrSql);
open;
end;