为什么MySQL选择日期不适用于不同的月份

时间:2022-02-08 01:30:37

I have a very old database I made when I was still learning PHP and MySQL. If I populate:

我有一个很旧的数据库,我在学习PHP和MySQL时做的。如果我填充:

SELECT * FROM Jobs WHERE (date BETWEEN 'Aug.01 2016' AND 'Aug.31 2016')

I can actually populate rows

我可以填充行

Found rows: 4 Warnings: 0 Duration for 1 query: 0.000 sec.

找到的行:4个警告:1个查询0持续时间:0.000秒。

But once I change my month (moving the range from Aug 1 to July 1):

但是一旦我改变了我的月份(从8月1日到7月1日):

SELECT * FROM Jobs WHERE (date BETWEEN 'Jul.01 2016' AND 'Aug.31 2016')

I got nothing even so the range July 1 ~ Aug 31 includes Aug 1 ~ Aug 31 (from previous example), which should populate at least 4 rows

7月1日~ 8月31日期间,我没有得到任何东西,包括8月1日~ 8月31日(之前的例子),应该至少填充4行。

Found rows: 0 Warnings: 0 Duration for 1 query: 0.016 sec.

发现行:0警告:1查询持续时间:0.016秒。

Is there a way I can populate the rows even they have different month?

是否有一种方法我可以填充行,即使它们有不同的月份?

1 个解决方案

#1


3  

You need to first convert your date string to a valid date:

您需要首先将您的日期字符串转换为一个有效的日期:

SET @date := 'Aug.01 2016';

SELECT STR_TO_DATE(@date,'%M.%d %Y');

Output(yyyy-mm-dd): 2016-08-01

输出(yyyy-mm-dd):2016-08-01

Now use this in your query to search for result having dates between 2016-07-01 & 2016-08-31.

现在,在您的查询中使用它来搜索日期为2016-07-01和2016-08-31的结果。

SELECT 
*
FROM Jobs 
WHERE STR_TO_DATE(date,'%M.%d %Y') BETWEEN '2016-07-01' AND '2016-08-31';

Note: But date should be stored in a date datatype column. It's high time you converted the datatype to date of your date column.

注意:但是日期应该存储在日期数据类型列中。现在是您将数据类型转换为date列的时候了。

In order to do that follow the steps below:

为了做到这一点,请遵循以下步骤:

ALTER TABLE Jobs
ADD new_date_column date;

UPDATE Jobs 
SET new_date_column = STR_TO_DATE(date,'%M.%d %Y');

ALTER TABLE Jobs DROP COLUMN `date`;

ALTER TABLE Jobs CHANGE COLUMN `new_date_column` `date` date;

Steps in a nut shell:

坚果壳中的步骤:

  1. Add a new column in Jobs table of type date.

    在类型为date的作业表中添加一个新的列。

  2. Now update this new column with the values from your date column after converting to a valid date.

    现在,在转换到一个有效日期之后,用date列中的值更新这个新的列。

  3. You don't need that date column. So drop it.

    你不需要那个日期栏。所以把它。

  4. Now rename the newly created column to date.

    现在将新创建的列重命名为date。

#1


3  

You need to first convert your date string to a valid date:

您需要首先将您的日期字符串转换为一个有效的日期:

SET @date := 'Aug.01 2016';

SELECT STR_TO_DATE(@date,'%M.%d %Y');

Output(yyyy-mm-dd): 2016-08-01

输出(yyyy-mm-dd):2016-08-01

Now use this in your query to search for result having dates between 2016-07-01 & 2016-08-31.

现在,在您的查询中使用它来搜索日期为2016-07-01和2016-08-31的结果。

SELECT 
*
FROM Jobs 
WHERE STR_TO_DATE(date,'%M.%d %Y') BETWEEN '2016-07-01' AND '2016-08-31';

Note: But date should be stored in a date datatype column. It's high time you converted the datatype to date of your date column.

注意:但是日期应该存储在日期数据类型列中。现在是您将数据类型转换为date列的时候了。

In order to do that follow the steps below:

为了做到这一点,请遵循以下步骤:

ALTER TABLE Jobs
ADD new_date_column date;

UPDATE Jobs 
SET new_date_column = STR_TO_DATE(date,'%M.%d %Y');

ALTER TABLE Jobs DROP COLUMN `date`;

ALTER TABLE Jobs CHANGE COLUMN `new_date_column` `date` date;

Steps in a nut shell:

坚果壳中的步骤:

  1. Add a new column in Jobs table of type date.

    在类型为date的作业表中添加一个新的列。

  2. Now update this new column with the values from your date column after converting to a valid date.

    现在,在转换到一个有效日期之后,用date列中的值更新这个新的列。

  3. You don't need that date column. So drop it.

    你不需要那个日期栏。所以把它。

  4. Now rename the newly created column to date.

    现在将新创建的列重命名为date。