【发布时间】:2016-02-15 08:47:02
【问题描述】:
我正在尝试在我的项目中实现可选搜索,该项目有五个可选输入值(CompanyID、PackageID、SubjectID、Fromdate 和 Todate)
我把mysql查询写成
select l.RefNo, l.LetterDate, l.CompanyID, l.Subject,
c.CompanyID,
c.CompanyCode,
p.PackageName, p.PackageID
from tblletter l,
tblcompany c,
tblpackage p
where
(companyID is null or l.CompanyID = companyID ) and
(packageID is null or l.PackageID = packageID) and
(subjectID is null or l.LetterID = subjectID);
但我无法得到结果,我需要一种语法将日期值发送为 null,我已尝试为
DateTime? fromdate=....
这个语法正确吗? 谢谢。
将我的代码更新为
select l.RefNo, l.LetterDate, l.CompanyID, l.Subject,
Case LetterType
When 1 then 'PSK'
When 2 then c.CompanyCode
End As FromCompany,
Case LetterType
When 1 then c.CompanyCode
When 2 then 'PSK'
End As ToCompany,
c.CompanyCode,
p.PackageID
from tblletter l,
tblcompany c ,
tblpackage p
where l.CompanyID = c.CompanyID and l.PackageID = p.PackageID and
l.CompanyID Like
case
when l.CompanyID = c.CompanyID and companyID > 0 then ('%' + LTRIM(companyID) + '%')
else '%%'
end
and l.PackageID Like
case
when l.PackageID = p.PackageID and packageID > 0 then ('%' + LTRIM(packageID) + '%')
else '%%'
end
and l.Subject =
case
when subjectName is not null then subjectName
else '%%'
end;
但仍然得到空表值...
the possible cases are
1). companyID = 30 or packageID = 0 or subjectName = null
2). companyID = 0 or packageID = 11 or subjectName = null
3). companyID = 0 or packageID = 0 or subjectName = 'sand'
4). companyID = 30 and packageID = 11 or subjectName = null
5). companyID = 30 or packageID = 0 and subjectName = 'sand'
如果值为“0”,则必须忽略(或) 等等……像……
【问题讨论】:
-
不要在查询中添加可选参数,而是尝试在代码中使用条件创建查询,以便最终查询只有所需的参数\
-
那么,是否需要编写五个单独的存储过程,每个参数一个? @manoj
-
不不,抱歉,我不知道这是一个存储过程,我以为你是直接从你的 c# 程序触发查询。
标签: c# mysql select optional-parameters