求一复合查询的算法

wdj79 2003-08-18 10:38:16
有五个EDIT控件为edit1,edit2,edit3,edit4,edit5分别代表5个查询项目输入.我在adoquery 中设置了5个param参数分别和五个edit控件的输入值关联.现在首先要确定EDIT控件内容是否为空,是则不对此项目查询,否则用SQL语句select * from where 对EDIT输入值进行复合查询,请问用什么算法能更简单?用IF嵌套实在是太麻烦!
...全文
69 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxinji 2003-08-19
  • 打赏
  • 举报
回复
如果字段为字符型,可以在查询条件中用%来处理 sql字符串为
'select * from 表名 where 字段1 like '+edit1.txt+'%'+' and '+字段2 like '+edit2.txt+'%'

不用判断
newsofter 2003-08-19
  • 打赏
  • 举报
回复
巧妙利用Tag值
var
i :integer;
设置每个Edit的tag值为1,2,3,4,5
for i:=0 to self.components.count-1 do
if self.components[i] is TEdit then
if (Self.Components[i] as TEdit).text<>'' then
case Self.components[i].tag of
1 : sql.add(format(' and field1=''%s''',[trim(edit1.text)]))
2 : sql.add(format(' and field2=''%s''',[trim(edit2.text)]))
...
5 : sql.add(format(' and field5=''%s''',[trim(edit5.text)]))

end;
zninger 2003-08-19
  • 打赏
  • 举报
回复
设一个全局变量s,在TEdit的OnChange中公用 s:=(Sender as TEdit).Text;
再串SQL语句
sql:='select * from '+s+' where...'

一共就这么两句话,应该很简单
bigtailwolf163 2003-08-19
  • 打赏
  • 举报
回复
zhangxinji()的方法很实用的。一般在开发中都这样做。
Zhymax 2003-08-18
  • 打赏
  • 举报
回复
sorry 少打几个字:sWhere := leftstr(sWhere,length(sWhere)-3);
Zhymax 2003-08-18
  • 打赏
  • 举报
回复
sWhere:string;
sql:string;

sql:='select * from tb_name '
sWhere := 'where';
if trim(edit1.text) <> '' then sWhere :=' param1;='''+ trim(edit1.text) +''' and';
if trim(edit2.text) <> '' then sWhere :=' param2;='''+ trim(edit2.text) +''' and';
...
if trim(sWhere) <> 'where'then
begin
leftstr(sWhere,length(sWhere)-3);//去掉最后一个'and'
sql := sql + sWhere;
end;


2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧