多字段查询的问题

tgtcn 2009-04-06 09:14:46
比如有个单表,其中有员工编码,姓名,工资,入厂日期这几个字段

我要达到的目的是:可以任意输入其中一个字段,或任意任合时,可以查询出符合的记录
比如我知道,编码其中一部分01,姓名的一部分:黄,那么就查询出编码包含01并且姓黄的记录出来,
双比如,我查询,姓黄的,入厂日期>2009-3-4的员工资料
总之是任意组合,输入一个条件就AND多一个条件,有没有好的思路,四个字段可以还可以用列举组合的方式,如何有10个条件,就要组合很多了。
...全文
96 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
coderee 2009-04-08
  • 打赏
  • 举报
回复
最常见的一个查询问题。
taste品味 2009-04-08
  • 打赏
  • 举报
回复
最关键的一句:
where 1=1

if edit1.text <> '' then
SQL.ADD('and a ''' + edit1.text + ''' ');

if edit2.text <> '' then
SQL.ADD('and b ''' + edit2.text + ''' ');

if edit3.text <> '' then
SQL.ADD('and c ''' + edit3.text + ''' ');

if edit4.text <> '' then
SQL.ADD('and d ''' + edit4.text + ''' ');

模式就这样,其它悠悠改改就可以了!套用吧!!
jinjazz 2009-04-08
  • 打赏
  • 举报
回复
select * from tb
where 1=1


然后由一个条件就写一个and

比如

and aa=bb
and cc=dd
之类的
tgtcn 2009-04-08
  • 打赏
  • 举报
回复
对不起,我理解错误了,可以了
tgtcn 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xbrave 的回复:]
select * from table where (编码 like ''%'+Edit1.text+'%''')and(姓名 like ''%'+Edit2.text+'%''')......
[/Quote]

请看清楚题目,如果第一个条件为空时,就查不出数据了。
bdmh 2009-04-07
  • 打赏
  • 举报
回复
4个条件对应4个Edit
判断Edit的值,然后组合SQL语句

SQL:string;

if Edit1.Text <> '' then
SQL := SQL +...
...//第一个条件

if Edit2.Text <> '' then
SQL := SQL +...
...//第一个条件

...
...
...
GDTOPONE 2009-04-06
  • 打赏
  • 举报
回复
1.员工编码,姓名,工资,入厂日期四个字段,本例用EDIT控件给用户输入查询条件
edit1.TEXT 对应员工编码字段
edit2.TEXT 对应姓名字段
edit3.TEXT 对应工资字段
edit4.TEXT 对应入厂日期字段

2.实现代码(已加入验证输入的工资是否是数据型,入厂日期是否是正确的日期格式):

procedure TForm1.Button1Click(Sender: TObject);
const
arr1: array[1..4] of string=(' and 员工编码=',' and 姓名=',' and 工资=',' and 入厂日期>');
var
i:Integer;
t:TDateTime;
s:string;
G_str:string;
j:Double;
begin
G_str:='select * from [员工信息表] where 1=1' ;
for I := 1 to 4 do
begin
s:=TEdit(Form1.FindComponent('Edit'+inttostr(i))).Text;
if i<4 then begin
if s<>'' then
if i=3 then begin
if TryStrToFloat(s,j) then
G_str:=G_str+arr1[i]+s
else begin
MessageBox(0,'输入的工资格式有误','错误',MB_ICONERROR);
Exit;
end;
end
else
G_str:=G_str+arr1[i]+QuotedStr(s);
end else begin
if s<>'' then

if TryStrToDateTime(s,t) then
G_str:=G_str+arr1[i]+QuotedStr(s)
else begin
MessageBox(0,'输入的入厂日期错误','错误',MB_ICONERROR);
Exit;
end;
end;
end;

with ADOQuery1 do begin
Close;
SQL.Text:=G_str;
Open;
end;
end;
ks_reny 2009-04-06
  • 打赏
  • 举报
回复

一下是我的多条件查询代码,楼主参考一下,我用的是combobox控件。

procedure Tquery1frm.Button1Click(Sender: TObject);
function AddPercent(sSource:string):string;
begin
result := sSource;
if Pos('%',sSource)=0 then Result := sSource+'%';
end;
var sqlstr,swhere:string;
begin
sqlstr:='Select PQI_NUMBER,PQI_SUPPLIER,PQI_OSPOP,PQI_CREATER,PQI_CREATEDATE,PQI_APPROVER,PQI_APPROVEDATE,PQI_CURRENCY,PQI_Status,PQI_TEXT,PQI_ID From tblospquota where PQI_NUMBER in(';
sqlstr:=sqlstr+'Select distinct PQI_NUMBER From tblospquota A,tblospquotadt B where A.PQI_ID=B.PQI_ID(+) ';
swhere:='';
if combobox1.Text <> '' then sWhere := sWhere + format(' AND A.PQI_NUMBER LIKE''%s'' ', [ AddPercent(combobox1.Text) ]);
if combobox2.Text <> '' then sWhere := sWhere + format(' AND A.PQI_SUPPLIER LIKE ''%s'' ', [ AddPercent(suppler) ]);
if combobox3.Text <> '' then sWhere := sWhere + format(' AND A.PQI_OSPOP LIKE ''%s'' ', [ AddPercent(combobox3.Text) ]);
if combobox4.Text <> '' then sWhere := sWhere + format(' AND B.QID_ITM_CODE LIKE ''%s'' ', [ AddPercent(combobox4.Text) ]);
if radiogroup1.ItemIndex<>-1 then sWhere := sWhere + format(' AND A.PQI_STATUS LIKE ''%s'' ', [ AddPercent(status) ]);
sqlstr:=sqlstr+swhere;
sqlstr:=sqlstr+')';
with quota_approve.quota_approvefrm.ADOQuery1 do
begin
close;
sql.Clear;
sql.Add(sqlstr);
open;
end;
end;
xbrave 2009-04-06
  • 打赏
  • 举报
回复
select * from table where (编码 like ''%'+Edit1.text+'%'')and(姓名 like ''%'+Edit2.text+'%'')......
xbrave 2009-04-06
  • 打赏
  • 举报
回复
select * from table where (编码 like ''%'+Edit1.text+'%''')and(姓名 like ''%'+Edit2.text+'%''')......

2,498

社区成员

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

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