您的位置:程序门 -> delphi ->



sql查询?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


sql查询?
发表于:2007-12-10 14:46:49 楼主
有个查询,例如
edit1,edit2,edit3,分别输入不同的查询内容如:产品类型、产品成本、日期,
如果edit1和edit2输入内容,edit3为空,则查询出与edit1和edit2相匹配的内容;
如果edit1、edit2、edit3输入内容,则查询出与之相匹配的内容;
如果edit1输入内容,则查询出与之相匹配的内容;
反正那个edit有内容,就查;如果没就不列入查询范围啊
发表于:2007-12-10 14:54:081楼 得分:0
在你的窗体类的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%)
括号中的%符号不能少
发表于:2007-12-24 07:51:0610楼 得分:0
c
发表于:2007-12-24 17:05:3011楼 得分:0
7楼正解
发表于:2007-12-24 18:14:4312楼 得分:0
ok
发表于:2007-12-24 19:11:5513楼 得分:0
不好意思,題目未看清楚,再修改一部分內容:

quotedstr(edit1.text)
quotedstr(edit2.text)
quotedstr(edit3.text)
改成:
quotedstr('%'   +   edit1.text   +   '%');  
quotedstr('%'   +   edit2.text   +   '%');  
quotedstr('%'   +   edit3.text   +   '%');  

 
发表于:2007-12-24 19:13:3914楼 得分:0
以上的是最簡單的多條件查詢,不管是多少個未知條件,都可動態生成sql語句,不會出錯的


快速检索

最新资讯
热门点击