我的数据表中有多个字段,比如生产时间,钢坯号,钢种,规格,操作班组等等,我想实现多条件查询,比如一段时间内,某个钢种,某种规格下的记录,但是这些条件中,除了时间段必须设定外,其他条件都是用户可选的,也就是查询条件的个数和组合都是用户任意选定的,这样在程序中如何实现呢?

解决方案 »

  1.   

      zch:='Select * From (select * from piece_history where 日期时间 between ' + #39 + DateToStr(DateTimePicker1.Date) + ' ' + timetostr(datetimepicker3.Time) + #39 + ' and ' + #39 + DateToStr(DateTimePicker2.Date) + ' ' + timetostr(datetimepicker4.Time) + #39 +') as temptable where 钢种='''+trim(combobox1.Text)+'''' ;
    这句话能够执行,可是没有结果,为什么呀?
      

  2.   

    zch:='select * from from piece_history where 日期时间 between ' + #39 + DateToStr(DateTimePicker1.Date) + ' ' + timetostr(datetimepicker3.Time) + #39 + ' and ' + #39 + DateToStr(DateTimePicker2.Date) + ' ' + timetostr(datetimepicker4.Time) + #39 +' and 钢种='''+trim(combobox1.Text)+'''';
      

  3.   

    用户选择什么条件,where 后面就加什么条件嘛
    sql="select * from where 1=1"
    if 选择了条件1 then 
    sql=sql+加条件1;
    if 选择了条件2 then 
    sql=sql+加条件2;
    .....
    if 选择了条件n then 
    sql=sql+加条件n;
      

  4.   

    拼SQL var
      strSql: string;
    begin
      strSql := ' select * from table where 1=1';
      if trim(edit1.text)<>'' then strSql := strSql + ' and a = :a';
      ......
      with adoquery1 do
      begin
        close;
        sql.text := strSql;
        if trim(edit1.text)<>'' then parameters.parambyname('a').value = trim
    (edit1.text);
        ......
        Open;
      end;
    end;