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

发布于 2024-12-24 22:57:41 字数 1805 浏览 0 评论 0原文

为什么此查询给出空集并带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷了相思 2024-12-31 22:57:41

您可以尝试使用 str_to_date 像这样

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

,看看这是否有效

You could try with str_to_date like this

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

and see if this works

兔姬 2024-12-31 22:57:41

试试这个:

select * from Productions where DATE(convert(datetime, data_prod)) = '01/01/2012'

Try this instead:

select * from Productions where DATE(convert(datetime, data_prod)) = '01/01/2012'
鹤仙姿 2024-12-31 22:57:41

使用 DATE()< /code>函数仅与 DATETIME 的日期部分进行比较。不要在 WHERE 子句中使用 DATE_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';

或者,STR_TO_DATE() 可以在 MySQL 中将日期转换为正确的格式,而不是在 Java 中的应用程序端进行。

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

Use the DATE() function to compare only against the date portion of DATETIME. Don't use DATE_FORMAT() in the WHERE clause, because you need to use MySQL's internal date format YYYY-MM-DD there:

/* 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';

Alternatively, STR_TO_DATE() can convert the date to the correct format in MySQL, rather than doing it application-side in Java.

select * from Productions where DATE(date_prod) = STR_TO_DATE('01/01/2012', '%d/%m/%Y');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文