使用 MySQL 数据库格式化日期时,为什么此查询给出空集?
为什么此查询给出空集并带有 3 个警告?
SELECT *
FROM Productions
WHERE date_prod=DATE_FORMAT('01/01/2012','%Y/%m/%d');
这就是生产表中的内容。
+-------------+------------+----------+----------+---------+---------------------+---------------------+---------------------+----------+
| 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);
Why this query Gives empty set with 3 warnings?
SELECT *
FROM Productions
WHERE date_prod=DATE_FORMAT('01/01/2012','%Y/%m/%d');
this is what in the Productions Table.
+-------------+------------+----------+----------+---------+---------------------+---------------------+---------------------+----------+
| 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)
I'm using a French OS (Windows XP) and we use this date format: DD/MM/YYYY
and this is the Java Code i'm using to fill the JTable
"select * from Productions where date_prod=STR_TO_DATE('"+jTextField2.getText()+"','%Y-%m-%d')"
The Table Structure
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试使用 str_to_date 像这样
,看看这是否有效
You could try with str_to_date like this
and see if this works
试试这个:
Try this instead:
使用
DATE()< /code>
函数仅与
DATETIME
的日期部分进行比较。不要在WHERE
子句中使用DATE_FORMAT()
,因为您需要在那里使用 MySQL 的内部日期格式 YYYY-MM-DD:或者,
STR_TO_DATE()
可以在 MySQL 中将日期转换为正确的格式,而不是在 Java 中的应用程序端进行。Use the
DATE()
function to compare only against the date portion ofDATETIME
. Don't useDATE_FORMAT()
in theWHERE
clause, because you need to use MySQL's internal date format YYYY-MM-DD there:Alternatively,
STR_TO_DATE()
can convert the date to the correct format in MySQL, rather than doing it application-side in Java.