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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值