为什么客户总是要这么多的查询???组合查询问题???CheckBox的用法。。。(100分)

  • 主题发起人 xuegaoyili
  • 开始时间
X

xuegaoyili

Unregistered / Unconfirmed
GUEST, unregistred user!
我做的程序要求能组合查询,就是在条件中的字段可要可不要。。。我用3个Checkbox控件
控制。如果选中就在查询中要满足这个字段的要求。。。比如有:年,月,日三个字段。。
如果checkbox1.checked=true,checkbox2.checked=false,checkbox3.checked=true的话
那么SQL为"select * from 表名 where year='2002' and day='20'“
如果checkbox1.checked=false,checkbox2.checked=true,checkbox3.checked=true的话
那么SQL为"select * from 表名 where month='12' and day='20'“
下面是代码:
//第一种组合:000
if checkBox3.Checked=false and checkBox4.Checked=false and checkBox5.Checked=false then
begin
mysql:='语句略'
end;
//第二种组合:100
if (checkBox3.Checked=true and checkBox4.Checked=false and checkBox5.Checked=false) then
begin
mysql:='...'
end;
//第三种组合:010
if (checkBox3.Checked=false and checkBox4.Checked=true and checkBox5.Checked=false) then
begin
mysql:='...'
end;
//第四种组合:001
if checkBox3.Checked=false and checkBox4.Checked=false and checkBox5.Checked=true then
begin
mysql:='...'
end;
.....
为什么不管我点不点CheckBox控件,程序会把所有的if 当成真来执行,使SQL语句永远
是最后一条,不会随着CheckBox控件的选择情况改变
 
if (checkBox3.Checked=true and checkBox4.Checked=false and checkBox5.Checked=false) then
改成
if (checkBox3.Checked=true) and (checkBox4.Checked=false) and (checkBox5.Checked=false) then
其它照改

注意语法,再说你这样也不是好的解决方法
 
你的方法不好,用我的吧:

假设你有十个条件可供组合:

if not(checkbox1.checked or checkbox2.checked or ... or checkbox10.checked) then exit;
strSQL='select * from mytable where ';

if checkbox1.checked then//年
strSQL:=strSQL+' year='+trim(edit1.text)+' and';

if checkbox2.checked then//月
strSQL:=strSQL+' month='+trim(edit2.text)+' and';

.......
......
.......

if checkbox10.checked then//id
strSQL:=strSQL+' id='''+trim(edit10.text)+''' and';

strSQL:=left(strSQL,length(strSQL)-3);

//strSQL:=strSQL+' order by id';//如果还要排序的话
 
用串来组合,很好啊!str1,str2,str3,sql.add(str1+str2+str3),
 
谢谢各位,散分。。。。
 
顶部