在你的窗体类的private区域声明如下三个方法: //名字依你的需要而定 function getcondition1: string; function getcondition2: string; function getcondition3: string;
//实现 function getcondition1: string; begin if sametext(trim(edit1.text), emptystr) then result := emptystr else result := ' and 产品类型 like ''%' + trim(edit1.text) + '%'''; end;
function getcondition2: string; begin if sametext(trim(edit1.text), emptystr) then result := emptystr else result := ' and 产品成本 like ''%' + trim(edit1.text) + '%'''; end;
function getcondition3: string; begin if sametext(trim(edit1.text), emptystr) then result := emptystr else result := ' and 日期 = ''' + trim(edit1.text) + ''''; end;
在你的查询方法中写: begin sql := 'select * from tablename where (1 = 1)' + getcondition1 + getcondition2 + getcondition3; query.close; query.sql.text := sql; query.open; ....... end;
发表于:2007-12-10 14:55:332楼 得分:0
arrstr:array[0..3] of string; i:integer; querystring:string; begin if trim(edit1) <> '' then arrstr[0]:=' (产品类型='''+edit1text+''')'; if trim(edit2.text) <> '' then arrstr[1]:=' (产品成本='+edit2.text+')'; if trim(edit3.text) <> '' then arrstr[2]:=' (日期='''+edit3.text+''')'; for i:=0 to 2 do begin if arrstr[i] <> '' then begin if arrstr[3] <> '' then arrstr[3]:=arrstr[3]+' and '+arrstr[i] else arrstr[3]:=arrstr[i]; end; end; if arrstr[3]='' then querystring:='select * from table' else querystring:='select * from table where '+ arrstr[3]; query.sql.text(querystring); query.open;
发表于:2007-12-10 15:03:513楼 得分:0
select 产品类型,产品成本,日期 form table where 产品类型 like ''%'+edit1.text+'%'' and 产品成本 like ''%'+edit2.text+'%'' and 日期 like ''%'+edit3.text+'%''
这就是个简码的查询,如果想查询的更快,用if来判断edit中是否有输入既可。
发表于:2007-12-10 16:42:414楼 得分:0
select 产品类型,产品成本,日期 from 表 where 产品类型 like ''%'+trim(edit1.text)+'%'' and 产品成本 like ''%'+trim(edit2.text)+'%'' and 日期 like ''%'+trim(edit3.text)+'%'' 需要判断日期的合法性!
发表于:2007-12-14 17:34:055楼 得分:0
var lssql, lscondition: string; begin lscondition := ''; lssql := 'select * from tablename '; if trim(edit1.text) then lscondition := lscondition + ' and 产品类型 like %' + trim(edit1.text) + '%'; if trim(edit2.text) then lscondition := lscondition + ' and 产品成本 like %' + trim(edit2.text) + '%'; if trim(edit3.text) then lscondition := lscondition + ' and 日期 = ' + trim(edit2.text);
if lscondition <> '' then begin lscondition := copy(lscondition, 5, length(lscondition)); lssql := lssql + 'where ' + lscondition; end; EXECute(lssql); end;
发表于:2007-12-14 17:35:286楼 得分:0
if trim(edit1.text) <> '' then
发表于:2007-12-22 14:36:037楼 得分:0
這個簡單: var strsql,strwhere:string; begin strsql:=' select * from table '; strwhere:=' where '; if edit1.text <> '' then begin strsql:= strsql + strwhere + ' 产品类型= ' + quotedstr(edit1.text); strwhere:=' and '; end; if edit2.text <> '' then begin strsql:= strsql + strwhere + ' 产品成本= ' + quotedstr(edit2.text); strwhere:=' and '; end; if edit3.text <> '' then begin strsql:= strsql + strwhere + ' 日期= ' + quotedstr(edit3.text); strwhere:=' and '; end; query.close; query.sql.clear; query.sql.add(strsql); query.open; end;
发表于:2007-12-22 15:05:498楼 得分:0
d
发表于:2007-12-24 00:42:309楼 得分:0
楼上的复杂了如下: select * from abc where a like (%edit1.text%) and b like (%edit2.text%) and c like(%edit3.text%) 括号中的%符号不能少