有combobox1,combobox2,combobox3,combobox4这样四个控件。想做一个查询。
如果让它们任意组合都能查到数据库中的内容。如何设计这个查询呀?寻找最好的办
法。我用的办法太笨了。初学DELPHI对程序设计也不是太懂。没思路。请多多指教。
不要嫌我太笨呀。

解决方案 »

  1.   

    这4个combobox连的字段如果为字符型字段
    用下面的方法
    query1.close;
    query1.sql.clear;
    query1.sql.add('select * from 表名 where 字段1 like '+''''+cobobox1.text+'%'+''''+'and 字段2 like '+''''+cobobox2.text+'%'+''''+'and 字段3 like '+''''+cobobox3.text+'%'+''''+'and 字段4 
    like '+''''+cobobox4.text+'%'+'''');
    query1.execsql;
    query1.open;
    我经常用以上方法做多条件查询
    可能效率要低一些,不过如果你的记录不会超过100万条是看不出来慢的
    你试试啊
    小弟也是初学者
    有机会多交流啊
      

  2.   

    sql语句十一行上的,
    字段名和like之间是有空格的
    别忘了啊
      

  3.   

    query1.close;
    query1.sql.clear;
    query1.sql.add('select * from table where 1=1 ');
    if combobox1.text<>'' then
      query1.sql.add(' and name='''+combobox1.text+'''');
    if combobox2.text<>'' then
      query1.sql.add(' and unit='''+combobox2.text+'''');
    if combobox3.text<>'' then
      query1.sql.add(' and sex='''+combobox3.text+'''');
    if combobox4.text<>'' then
      query1.sql.add(' and address='''+combobox4.text+'''');
    query1.open;