一个有关SQL语句问题。。。

假设 我有五个参数 带入到SQL中当作五个条件去查询,
如果没有查到数据则去掉一个我指定的条件,
如果还是没有查到则继续去掉一条件,以此类推,直到有结果返回
如何在一条SQL上实现这样的效果

在条件前加 if判断;if(条件是否存在)(...)


select * from a 
where (a.条件1=#{con1} and a.条件2=#{con2} and a.条件3=#{con3} and a.条件4= #{con4} and a.条件5= #{con5}) 
or (a.条件1=#{con1} and a.条件2=#{con2} and a.条件3=#{con3} and a.条件4= #{con4}) 
or (a.条件1=#{con1} and a.条件2=#{con2} and a.条件3=#{con3})
or(a.条件1=#{con1} and a.条件2=#{con2})
or(a.条件1=#{con1})

where 后面的条件利用for进行遍历拼接

还是应用层做判断吧,每次去掉一个条件做查询感觉这样要简单一点

你可以这样,我用我有的表 当例子啊
select * from gy_user where department_id = '200905'

UNION

select * from gy_user where department_id = '200903' and (select count(1) from gy_user where department_id = '200905')=0

不过你这样的话 你的SQL得写的老长了