Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在where条件下测试日期是否为空_Mysql_Sql_Prepared Statement_Where - Fatal编程技术网

Mysql 如何在where条件下测试日期是否为空

Mysql 如何在where条件下测试日期是否为空,mysql,sql,prepared-statement,where,Mysql,Sql,Prepared Statement,Where,我需要测试在where条件下日期是否不为空: String query = "SELECT * FROM employe WHERE date IS NOT NULL date > ? AND date IS NOT NULL date < ? "; PreparedStatement statement = connessione_db.prepareStatement(query); statement.setDate(1, data_interval_1); statement

我需要测试在where条件下日期是否不为空:

String query = "SELECT * FROM employe WHERE date IS NOT NULL date > ? AND date IS NOT NULL date < ? ";
PreparedStatement statement = connessione_db.prepareStatement(query);
statement.setDate(1, data_interval_1);
statement.setDate(1, data_interval_2);
String query=“从员工中选择*日期不为空日期>?且日期不为空日期<?”;
PreparedStatement=connessione_db.prepareStatement(查询);
语句.setDate(1,数据间隔1);
语句.setDate(1,数据间隔2);

date
必须介于
data\u interval\u 1
data\u interval\u 2
之间,
data\u interval\u 1
data\u interval\u 2
可以为空。

为了测试
date
是否不为空,您可以执行以下操作:

where date is not null
但是,对于条件,您可以执行以下操作:

where date >= ? and date <= ?

where date>=?日期您不需要检查
NULL
,因为如果日期是
NULL
,范围检查将自动失败。请尝试以下版本:

String query = "SELECT * FROM employe WHERE date > ? AND date < ?";
PreparedStatement statement = connessione_db.prepareStatement(query);
statement.setDate(1, data_interval_1);
statement.setDate(2, data_interval_2);
String query=“从日期>和日期<的员工中选择*”;
PreparedStatement=connessione_db.prepareStatement(查询);
语句.setDate(1,数据间隔1);
语句.setDate(2,数据间隔2);
注意:在上面的代码中,您将两个间隔日期绑定到同一位置。您希望绑定到职位1和职位2。

从日期不为空的员工中选择*,并且(日期>和日期<?)
SELECT * FROM employe WHERE date IS NOT NULL AND (date > ? AND date < ?)
我们首先检查日期是否为空。
如果此条件为真,则它将检查日期是否在范围内。

仅供参考,最好使用存储过程,而不是在java代码中硬编码SQL语句。参考: