MySQL语法检查参数是否为空 [英] MySQL syntax checking if parameter is null

查看:257
本文介绍了MySQL语法检查参数是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来执行MySQL语句检查给定参数是否存在。我记得我可以在Oracle中执行以下操作来实现该目标:

I am looking for the way to execute MySQL statement checking if given parameter exists. As I remember I can do the following in Oracle to achieve that:

select s.* from Site s 
where s.study = :study
and (:enabled is null or s.enabled = :enabled)

在MySQL中也可能有类似的事情吗?相同的代码执行没有错误,但是从不返回任何记录。

is anything like that possible in MySQL too? The same code executes without error but never return any records.

我的目标是避免Java代码中出现多个lf和else。当启用的参数为null时,查询应该看起来像这样:

My goal here is to avoid multiple lfs and elses in my java code. It should work the way that the query looks like that when enabled parameter is null:

select s.* from Site s 
where s.study = :study

,如果参数不为空,则类似:

and like that if the parameter is not null:

select s.* from Site s 
where s.study = :study
and s.enabled = :enabled

,而我想通过一个查询来做到这一点

and I want to do that with a single query

推荐答案

不幸的是,它高度依赖于数据库驱动程序。我的初始查询在数据库工具中运行时有效,但在JPA上运行时则不必如此。因此,我要结束这个问题,因为它不需要进一步的答案。抱歉,小伙子们浪费了您的时间。

Unfortunately it is highly dependent on database driver. My initial query works when run in database tools but doesn't have to when it comes to run it by JPA. So I'm to close this question as it doesn't require further answers. I'm sorry lads for wasting your time.

这篇关于MySQL语法检查参数是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆