Java 为什么在使用MySQL数据库格式化日期时,此查询会给出一个空集?

Java 为什么在使用MySQL数据库格式化日期时,此查询会给出一个空集?,java,mysql,sql,date,date-format,Java,Mysql,Sql,Date,Date Format,为什么此查询会给出带有3个警告的空集合 SELECT * FROM Productions WHERE date_prod=DATE_FORMAT('01/01/2012','%Y/%m/%d'); 这是Productions表中的内容 +-------------+------------+----------+----------+---------+---------------------+---------------------+---------------------+--

为什么此查询会给出带有3个警告的空集合

SELECT * 
FROM Productions 
WHERE date_prod=DATE_FORMAT('01/01/2012','%Y/%m/%d');
这是Productions表中的内容

+-------------+------------+----------+----------+---------+---------------------+---------------------+---------------------+----------+
| numlotfdg   | date_prod  | Dur_Prod | Dur_trns | Dur_ins | t0                  | tc                  | texp                | nbrdoses |
+-------------+------------+----------+----------+---------+---------------------+---------------------+---------------------+----------+
| FG-111204B1 | 2011-12-04 |      180 |       70 |      30 | 2011-12-04 06:20:00 | 2011-12-04 10:30:00 | 2011-12-04 16:30:00 |        6 |
| FG-111227B1 | 2011-12-27 |      180 |       50 |      30 | 2011-12-27 06:40:00 | 2011-12-27 10:00:00 | 2011-12-27 16:45:00 |       18 |
| FG-120101B1 | 2012-01-01 |        0 |       70 |      30 | 2012-01-01 06:20:00 | 2012-01-01 10:30:00 | 2012-01-01 16:30:00 |        6 |
+-------------+------------+----------+----------+---------+---------------------+---------------------+---------------------+----------+
3 rows in set (0.00 sec)
我使用的是法国操作系统(Windows XP),我们使用的日期格式为:
DD/MM/YYYY

这是我用来填充JTable的Java代码

"select * from Productions where date_prod=STR_TO_DATE('"+jTextField2.getText()+"','%Y-%m-%d')"
表结构

create table Productions(numlotfdg varchar(100) primary key,
                        date_prod Date,
                        Dur_Prod int,
                        Dur_trns int,
                        Dur_ins int ,
                        t0 Timestamp,
                        tc TimeStamp,
                        texp Timestamp,
                        nbrdoses int);
使用该函数仅与
DATETIME
的日期部分进行比较。不要在
WHERE
子句中使用
DATE\u FORMAT()
,因为您需要在那里使用MySQL的内部日期格式YYYY-MM-DD:

/* Truncate the DATETIME field to only its date for comparison */
/* And use the format YYYY-MM-DD in the WHERE clause */
select * from Productions where DATE(date_prod) = '2012-01-01';
或者,您可以在MySQL中将日期转换为正确的格式,而不是在Java应用程序端进行转换

select * from Productions where DATE(date_prod) = STR_TO_DATE('01/01/2012', '%d/%m/%Y');
使用该函数仅与
DATETIME
的日期部分进行比较。不要在
WHERE
子句中使用
DATE\u FORMAT()
,因为您需要在那里使用MySQL的内部日期格式YYYY-MM-DD:

/* Truncate the DATETIME field to only its date for comparison */
/* And use the format YYYY-MM-DD in the WHERE clause */
select * from Productions where DATE(date_prod) = '2012-01-01';
或者,您可以在MySQL中将日期转换为正确的格式,而不是在Java应用程序端进行转换

select * from Productions where DATE(date_prod) = STR_TO_DATE('01/01/2012', '%d/%m/%Y');
请尝试以下方法:

select * from Productions where DATE(convert(datetime, data_prod)) = '01/01/2012'
请尝试以下方法:

select * from Productions where DATE(convert(datetime, data_prod)) = '01/01/2012'
你可以这样试一下

select * from Productions where date_prod=STR_TO_DATE('01/01/2012','%d/%m/%y');
看看这是否管用

你可以这样试一下

select * from Productions where date_prod=STR_TO_DATE('01/01/2012','%d/%m/%y');

看看这是否有效

Nope,它给出空集,1个警告(0.05秒)Nope,它给出空集,1个警告(0.05秒)
是否从制作中选择日期(DATE_prod)
工作正常?空集,1个警告(0.00秒)Ok,再编辑一次,它工作了吗?如果没有,则从制作中选择日期(转换(日期时间,日期生产))是否正常工作?从制作中选择日期(日期生产)是否正常工作?空集,1个警告(0.00秒)正常,再编辑一次,是否正常工作?如果没有,那么
从产品中选择日期(convert(datetime,DATE\u prod))是否正常工作。。。认真地说,在Java代码中使用
PreparedStatement
s。。。认真地说,使用
PreparedStatement
s.@Papa\u Jay然后使用
STR\u TO\u DATE()
,就像我上面添加的那样。@Papa\u Jay然后使用
STR\u TO\u DATE()