比如当选着“全部”的时候 sql 中的查询语句 a.GongSiNo = '"&request("gongsi")&"' 不应该有!
现在的语句是这样的!
select a.gongsimc,a.card_kehu_no as kehu_no,a.card_no,a.card_kehu_mc as kehu_mc,a.card_kehu_shouji as kehu_dh,a.che_no,a.card_kind,a.card_enddate,lastdate,datediff(day,isnull(lastdate,getdate()-365),getdate()) days from (select zhifu_card_no,max(lastdate) as lastdate from (select zhifu_card_no ,xche_jsrq lastdate from work_pz_sj union select zhifu_card_no,xc_rq lastdate from work_xiche_pz_sj union select zhifu_card_no,xiao_rq lastdate from xiaosh_pz_sj union select card_no as zhifu_card_no,xche_jsrq lastdate from work_pz_sj union select card_no as zhifu_card_no,xc_rq lastdate from work_xiche_pz_sj union select card_no as zhifu_card_no,xiao_rq lastdate from xiaosh_pz_sj) aa group by zhifu_card_no) b left join card a on b.zhifu_card_no=a.card_no where a.card_no is not null and lastdate>='"&request("txtBeginDate")&"' and lastdate<='"&request("txtEndDate")&"' and a.GongSiNo = '"&request("gongsi")&"' and datediff(day,isnull(lastdate,getdate()-365),getdate())>='"&request("txtDay")&"' and datediff(day,isnull(lastdate,getdate()-365),getdate())<='"&request("txtDay2")&"' ORDER BY lastdate
------解决方案--------------------
后台判断 当全部时 直接SQL
当非全部时 SQL+“and a.GongSiNo = '"&request("gongsi")&"'”
SQL=
select a.gongsimc,a.card_kehu_no as kehu_no,
a.card_no,a.card_kehu_mc as kehu_mc,
a.card_kehu_shouji as kehu_dh,
a.che_no,
a.card_kind,
a.card_enddate,
lastdate,
datediff(day,isnull(lastdate,getdate()-365),getdate()) days
from (
select zhifu_card_no,
max(lastdate) as lastdate
from (
select zhifu_card_no ,
xche_jsrq lastdate
from work_pz_sj
union
select zhifu_card_no,
xc_rq lastdate
from work_xiche_pz_sj
union
select zhifu_card_no,
xiao_rq lastdate
from xiaosh_pz_sj
union
select card_no as zhifu_card_no,
xche_jsrq lastdate
from work_pz_sj
union
select card_no as zhifu_card_no,
xc_rq lastdate
from work_xiche_pz_sj
union
select card_no as zhifu_card_no,
xiao_rq lastdate
from xiaosh_pz_sj
) aa
group by zhifu_card_no
) b
left join card a
on b.zhifu_card_no=a.card_no
where a.card_no is not null
and lastdate>='"&request("txtBeginDate")&"'
and lastdate<='"&request("txtEndDate")&"'
and datediff(day,isnull(lastdate,getdate()-365),getdate())>='"&request("txtDay")&"'
and datediff(day,isnull(lastdate,getdate()-365),getdate())<='"&request("txtDay2")&"'
ORDER BY lastdate
------解决方案--------------------
可以考虑把参数默认值为null,然后代码都写上:
select * from tb
where a=isnull(@a,a) and b=isnull(@b,b)
这样,如果前端没有选,就传入null值,因为where a=a恒为真,所以不会进行筛选操作
------解决方案--------------------
是这样,不过稍微改一下:
sql="1=1"
if request("条件1")<>"" then sql=sql&" and 查询字段1='"&request("条件1")&"'"
if request("条件2")<>"" then sql=sql&" and 查询字段2='"&request("条件2")&"'"
if request("条件3")<>"" then sql=sql&" and 查询字段3='"&request("条件3")&"'"
if request("条件4")<>"" then sql=sql&" and 查询字段4='"&request("条件4")&"'"
if request("条件5")<>"" then sql=sql&" and 查询字段5='"&request("条件5")&"'"