代码如下:
procedure Tf_info.cxButton1Click(Sender: TObject);
begin  if (trim(cxtextedit1.Text )='') and  (trim(cxtextedit2.Text )='') and
     (trim(cxtextedit3.Text )='') and   (trim(cxtextedit4.Text )='')
     then
     begin
     application.MessageBox('请输入查询条件','提示',64);
     cxtextedit2.SelectAll ;
     cxtextedit2.SetFocus ;
     exit;
     end
  else
  begin
  with DM.ADOQuery1 do
  begin
   close;
   sql.Clear   ;
   sql.Add('select   [itemid],[itemname],[itemspecs],[itemunit],[category],[memo],[date] from  itemaccoun where [itemid] like'+QuotedStr('%'+Trim(cxtextedit1.Text)+'%')) ;
   sql.Add ( 'and [itemname] like'+QuotedStr('%'+Trim(cxtextedit2.Text)+'%') );
   sql.Add( 'and [itemspecs] like'+QuotedStr('%'+Trim(cxtextedit3.Text)+'%') );
   sql.Add(  'and [memo] like '+QuotedStr('%'+Trim(cxtextedit4.Text )+'%'));
   dm.ADOQuery1.Active :=true;
   open;
      fields[0].DisplayLabel :='物料代码';//设置中文标题
      fields[1].DisplayLabel :='名称';
      fields[2].DisplayLabel :='规格描述';
      fields[3].DisplayLabel :='单位';
      fields[4].DisplayLabel :='单位';
      fields[5].displaylabel :='备注';
      fields[6].DisplayLabel :='创建日期';
    end;
    dbgrid1.Columns[0].Width :=70;//设置列宽
    dbgrid1.Columns[1].Width :=110;
    dbgrid1.Columns[2].Width :=200;
    dbgrid1.Columns[3].Width :=40;
    dbgrid1.Columns[4].Width :=60;
    dbgrid1.Columns[5].Width :=90;
    dbgrid1.Columns[6].Width :=100;
 end;
end;
我遇到的麻烦是上面这个条件出来的结果不对,有的查不到,可数据确实有,语法也没报错
 sql.Add('select   [itemid],[itemname],[itemspecs],[itemunit],[category],[memo],[date] from  itemaccoun where [itemid] like'+QuotedStr('%'+Trim(cxtextedit1.Text)+'%')) ;
   sql.Add ( 'and [itemname] like'+QuotedStr('%'+Trim(cxtextedit2.Text)+'%') );
   sql.Add( 'and [itemspecs] like'+QuotedStr('%'+Trim(cxtextedit3.Text)+'%') );
   sql.Add(  'and [memo] like '+QuotedStr('%'+Trim(cxtextedit4.Text )+'%'));
   dm.ADOQuery1.Active :=true;
   open;
如果把条件设一个就可以查到,四个条件中的任何一个都能查到,一起用就不行,有的存在的记录查不到
比如:
sql.Add('select   [itemid],[itemname],[itemspecs],[itemunit],[category],[memo],[date] from  itemaccoun where [itemid] like'+QuotedStr('%'+Trim(cxtextedit1.Text)+'%')) ;
不知道这个什么问题?到底是哪里出现了问题?

解决方案 »

  1.   

    你用的and必须WHERE子句里的几个字段都满足条件才能查的到 如果是做模糊查询 改成or
    如果不是这样 帖几条数据来看看
      

  2.   

    我连接的是sqlserver数据库.不能用or,用Or就没有意义了,越查越模糊,不会一步步精确了
      

  3.   

    用跟踪调试,将组合后的sql语句找到,然后粘贴到查询分析器中执行下,然后再分析结果,问题肯定能找到
      

  4.   

    1.ShowMessage出拼组的SQL
    这样的程序很容易被注入~
    建议过滤一些非法字符
      

  5.   

    我查到问题出在哪了,请各位高手再协助会诊一下:
    问题出在了字段是NULL的用组合查询差不多
    比如:
    select  * from  screencode where [itemid] like '%1601%' and [memo] like '%%'
    如果[Memo]字段没有值是NULL,那么用这条语句就差不出来东西对于这种问题,请教高手上面的代码得怎么个写法?
      

  6.   

    如果Memo为空时,你在拼组SQL语句时就不应该加and [memo] like这项
      

  7.   

    可是memo字段并不是都为空,有的是有值,还是需要like判断的,SQL语句我已经写出来了如下
    select  [itemid],[itemname],[itemspecs],[itemunit],[category],[memo],[date] from  itemaccoun where itemid like '%%'
    and [itemname] like'%%'
    and ([itemspecs] like '%%' or itemspecs is null)
    and ([memo] like '%%' or memo is null)上面的sql语句如果用在DELPHI里该怎么写?
      

  8.   

    SQL.Text:='select     [itemid],[itemname],[itemspecs],[itemunit],[category],[memo],[date]   from     itemaccoun   where   itemid   like   '+Quotedstr('%%')
    +' and   [itemname]   like'+QuotedStr('%%')
    +' and   ([itemspecs]   like   '+quotedstr('%%')+'   or   itemspecs   is   null)'
    +' and   ([memo]   like   '+Quotedstr('%%')+'   or   memo   is   null) '; 
      

  9.   

    谢谢热心的大伙,这个问题已经解决了, 荡尽尘埃的答案是对的,不过下面这种写法更好,用isnull处理begin
       close;
       sql.Clear   ;
       sql.Add('select   [itemid],[itemname],[itemspecs],[itemunit],[category],[memo],[date] from  itemaccoun where [itemid] like'+QuotedStr('%'+Trim(cxtextedit1.Text)+'%')) ;
       sql.Add ( 'and isnull([itemname],'''') like'+QuotedStr('%'+Trim(cxtextedit2.Text)+'%') );
       sql.Add('and isnull([itemspecs],'''') like'+QuotedStr('%'+Trim(cxtextedit3.Text)+'%')) ;
       sql.Add('and isnull([memo],'''') like '+QuotedStr('%'+Trim(cxtextedit4.Text)+'%'));
       dm.ADOQuery1.Active :=true;
       open;