我在做的程序中,有一个查询方式,但查询的条件有四个,但这四个条件只有一个是必须的,其它的随机选择要不要输入,但是在输入后也要能查出来,请问怎么写SQL语句?
比如:edit1、combobox1、combobox2、combobox3,这四个控件内输入的就是查询的条件,edit1是必须的。
比如:edit1、combobox1、combobox2、combobox3,这四个控件内输入的就是查询的条件,edit1是必须的。
adodataset.close
Adodataset.commandtext:='select * from A where 字段名='+''''+Edit1.text+''''+
and ((字段名='+''''+combobox1.text+'''') or
(字段名='+''''+combobox2.text+'''') or
(字段名='+''''+combobox3.text+''''))
adodataset.open;
你去试试看可以吗
select dfxx.zhh as 总户号,jbxx.dh as 段号,dfxx.nf as 年份,dfxx.yf as 月份,dfxx.je as 金额,dfxx.znj as 滞纳金,dfxx.je+dfxx.znj as 合计,dfxx.sfqf as 是否欠费,jbxx.hm as 户名 from dfxx,jbxx where (jbxx.dh=:edit1.text and dfxx.zhh=jbxx.zhh) or (jbxx.dh=:edit1.text and jbxx.sffs=:combobox1.text and dfxx.zhh=jbxx.zhh) or (jbxx.dh=:edit1.text and jbxx.sffs=:combobox1.text dfxx.nf=:combobox2.text and dfxx.zhh=jbxx.zhh) or (jbxx.dh=:edit1.text and jbxx.sffs=:combobox1.text dfxx.nf=:combobox2.text and dfxx.yf=:combobox3.text and dfxx.zhh=jbxx.zhh)
说明一下:有两张表,分别是dfxx和jbxx,条件是分别满足jbxx的dh、sffs和dfxx的nf和yf,但是效果不好,查询一次要好长时间,有时还老不出来。我怀疑是我的语句写得太麻烦了吧。所以想请教各位高手们,帮个忙。
sql:string;
begin
sql :=select dfxx.zhh as 总户号,jbxx.dh as 段号,dfxx.nf as 年份,dfxx.yf as 月份,dfxx.je as 金额,dfxx.znj as 滞纳金,dfxx.je+dfxx.znj as 合计,dfxx.sfqf as 是否欠费,jbxx.hm as 户名 from dfxx,jbxx where (jbxx.dh=:edit1.text) and xx.zhh=jbxx.zhh ';
if combobox1.text <> '' then
sql := sql + ' and jbxx.sffs=:combobox1.text';
if combobox2.text <> '' then
sql := sql + ' and dfxx.nf=:combobox2.text';
if combobox3.text <> '' then
sql := sql + ' and dfxx.yf=:combobox3.text';
adoquery.sql.text := sql;
adoquery.open;
end;
begin
close;
sql.clear;
sql.add('select * from dfxx inner join jbxx on dfxx.zhh=jbxx.zhh');
sql.add(sql+'where 1=1');if edit1.text<>'' then sql.add(' and jbxx.dh='''+edit1.text+'''');
if combobox1.text<>'' then sql.add(' and jbxx.sffs='''+combobox1.text+'''');
if combobox2.text<>'' then sql.add(' and dfxx.nf='''+combobox2.text+'''');
if combobox3.text<>'' then sql.add(' and dfxx.yf='''+combobox3.text+'''');
open;
end;
if edit1.text<>'' then sqltxt:=sqltxt+' and jbxx.dh='''+edit1.text+'''';
if combobox1.text<>'' then sqltxt:=sqltxt+' and jbxx.sffs='''+combobox1.text+'''';
if combobox2.text<>'' then sqltxt:=sqltxt+' and dfxx.nf='''+combobox2.text+'''';
if combobox3.text<>'' then sqltxt:=sqltxt+' and dfxx.yf='''+combobox3.text+'''';
Query.close;
Query.sql.clear;
Query.sql.add(SQLtxt);
Query.open;
if CheckBox1.Checked then Sqlstr:=sqlstr+' and 你的查询条件1';
if CheckBox2.Checked then Sqlstr:=sqlstr+' and 你的查询条件2';
if CheckBox3.Checked then Sqlstr:=sqlstr+' and 你的查询条件3';
if CheckBox4.Checked then Sqlstr:=sqlstr+' and 你的查询条件4';
if CheckBox5.Checked then Sqlstr:=sqlstr+' and 你的查询条件5';
if CheckBox6.Checked then Sqlstr:=sqlstr+' and 你的查询条件6';
if CheckBox7.Checked then Sqlstr:=sqlstr+' and 你的查询条件7';
if CheckBox8.Checked then Sqlstr:=sqlstr+' and 你的查询条件8';
if CheckBox9.Checked then Sqlstr:=sqlstr+' and 你的查询条件9';
if CheckBox10.Checked then Sqlstr:=sqlstr+' and 你的查询条件10';