满足多个条件的查询应该怎么做?!

shawnlee 2003-06-19 11:14:35
本人要用多个combobox来选择以后(有的可选,有的可不选),在DBGrid中显示出符合条件的选项。问题是:不知道如果让它判断出如果combobox(其中一个)为空值时,就跳过该空的combobox。本人用的是SQL语句。

datamodule4.adoquery1.Close;
datamodule4.ADOQuery1.SQL.Clear;
begin
if (combobox6.ItemIndex<>-1) then
a:=combobox6.Items[combobox6.itemindex];
if (combobox8.ItemIndex<>-1) then
c:=combobox8.Items[combobox8.itemIndex];
if (combobox9.ItemIndex<>-1) then
d:=combobox9.Items[combobox8.itemIndex];
if (combobox10.ItemIndex<>-1) then
e:=combobox10.Items[combobox8.itemIndex];
if (combobox11.ItemIndex<>-1) then
f:=combobox11.Items[combobox8.itemIndex];
if (combobox12.ItemIndex<>-1) then
g:=combobox12.Items[combobox8.itemIndex];

datamodule4.ADOQuery1.SQL.Add('select * from 学生信息 where 性别 like'''+a+''' or 民族 like'''+c+'''or 学生类型 like'''+d+''' or 院系 like'''+e+''' or 政治面貌 like'''+f+''' or 专业 like'''+g+'''');
...全文
16 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qzg 2003-06-19
  • 打赏
  • 举报
回复
你可以这样做
var
Strginsql:String;
begin
Stringsql:='select * from table where 2>1' //这里2>1是为了不判断AND语句
if a<>'' then
Stringsql:=StringSql+' and 民族='+''''+aaaa+'''';
if b<>'' then
Stringsql:=StringSql+' and 民族='+''''+bbbb+'''';
if c<>'' then
Stringsql:=StringSql+' and 民族='+''''+cccc+'''';
if d<>'' then
Stringsql:=StringSql+' and 民族='+''''+dddd+'''';
if a<>'' then
Stringsql:=StringSql+' and 民族='+''''+aaaa+'''';
剩下的往里边加。多少个都可以。
end;
hammer_shi 2003-06-19
  • 打赏
  • 举报
回复
那你还是拼成sql语句的字符串再datamodule4.ADOQuery1.SQL.Add(sqltext);

2,497

社区成员

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

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