java自定义sql查询条件_java – 如何编写条件可选的sql查询?

如果你可以

switch to named parameters,你可以将条件更改为检查参数为null,如下所示:

select e.event_id,a.attempt_id,a.preferred,a.duration,a.location

from event e,attempt a

where

(:ul is null OR e.user_label=:ul)

and (:st is null OR e.start_time=:st)

and (:et is null OR e.end_time=:et)

and (:dmin is null OR e.duration_min=:dmin)

and (:dmax is null OR e.duration_max=:dmax)

and e.event_id=a.event_id

如果你不能切换到命名参数,你仍然可以使用相同的技巧,但你需要为每个可选的参数传递两个参数:如果第二个参数设置,则该对的第一个参数为1,如果第二个参数,则为0一个省略:

select e.event_id,a.attempt_id,a.preferred,a.duration,a.location

from event e,attempt a

where

(? = 1 OR e.user_label=?)

and (? = 1 OR e.start_time=?)

and (? = 1 OR e.end_time=?)

and (? = 1 OR e.duration_min=?)

and (? = 1 OR e.duration_max=?)

and e.event_id=a.event_id


版权声明:本文为weixin_34108829原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。