简体   繁体   English

Mysql日期间隔

[英]Mysql date interval

I am trying to select rows which are within a certain time period: 我正在尝试选择在特定时间段内的行:

$sql = mysql_query(" SELECT * FROM table WHERE ... AND 
       date BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
       OR date ISNULL");

The format of the dates saved in this column are mm/dd/yyyy 此列中保存的日期格式为mm / dd / yyyy

a. 一种。 how do I select the time interval? 如何选择时间间隔? currently it is selecting all the rows, not just those within this date range. 目前它正在选择所有行,而不仅仅是这个日期范围内的那些行。

b. Am I using the corret syntax to make sure we include all null options as well? 我是否使用corret语法来确保包含所有null选项?

Update. 更新。

Following the answers and comments I changed the column type to "Date" (it was varchar), and have also changed the current type of the existing entries to the correct date format. 在答案和注释之后,我将列类型更改为“Date”(它是varchar),并且还将现有条目的当前类型更改为正确的日期格式。

I now have 2 questions: a. 我现在有两个问题:a。 This is still not working... What else might be the problem? 这仍然不起作用......还有什么可能是问题? b. I want to change in PHP where the entry is saved. 我想在PHP中更改保存条目的位置。 How ahould I go about this? 我该怎么办呢? Is the following correct: 以下是正确的:

 $new_date = mysql_query("STR_TO_DATE($old_date_string,'%m/%d/%Y')");

Solution. 解。

First, I changed the column type to DATE (from VARCHAR). 首先,我将列类型更改为DATE(来自VARCHAR)。

Second, I moved and converted all the existing entries from their existing column and type (str) to new type (date) and new column. 其次,我移动并将所有现有条目从其现有列和类型(str)转换为新类型(日期)和新列。

Third, I added parentheses to the "date" section. 第三,我在“日期”部分添加了括号。

Fourth, I changed ISNULL to IS NULL - addin a space, which changes it from a function to a statement. 第四,我将ISNULL更改为IS NULL - 添加一个空格,将其从函数更改为语句。

Fifth, I am now updating my script so that in the future dates will be saved to the new column in the new type, converting them to the correct fprmat before saving them to the database. 第五,我现在正在更新我的脚本,以便将来日期保存到新类型的新列中,在将它们保存到数据库之前将它们转换为正确的fprmat。

Thankyou @nnicholas and everybody. 谢谢@nnicholas和大家。

If you are really storing your dates as strings I suggest you update your table with something like the following - 如果您确实将日期存储为字符串,我建议您使用以下内容更新表格 -

-- add a new column of type DATE
ALTER TABLE `table` ADD COLUMN `date_new` DATE AFTER `date`;
-- populate the new column from the old one
UPDATE `table` SET `date_new` = STR_TO_DATE(`date`,'%m/%d/%Y');
-- drop the old column
ALTER TABLE `table` DROP COLUMN `date`;
-- rename the new column
ALTER TABLE `table` CHANGE `date_new` `date` DATE;

After making these changes handling dates will be much easier for you. 完成这些更改后,处理日期对您来说将更加容易。 When inserting the dates that you are receiving you simply use STR_TO_DATE('new_value','%m/%d/%Y') to convert the date during the insert - 插入您收到的日期时,只需使用STR_TO_DATE('new_value','%m/%d/%Y')转换插入期间的日期 -

mysql_query("INSERT INTO `table` (field1, field2, `date`) 
               VALUES('value1', 'value2', STR_TO_DATE('$old_date_string','%m/%d/%Y')");

This assumes that $old_date_string has already been sanitised. 这假设$ old_date_string已经被清理。

With your table structure updated the standard date arithmetic functions will work as intended. 随着表结构的更新,标准日期算术函数将按预期工作。

$sql = mysql_query("SELECT *
                    FROM table
                    WHERE ...
                    AND (`date` BETWEEN ('$date' - INTERVAL 2 YEAR) AND ('$date' + INTERVAL 2 YEAR) OR `date` IS NULL)");

$date must be in Ymd format. $date必须采用Ymd格式。

You can't do date comparisons on it if it's not stored in the database as a date. 如果它没有作为日期存储在数据库中,则无法对其进行日期比较。

You could do something very ugly casting it to a date with STR_TO_DATE() and then comparing but I'd suggest reworking your table structure to store this date as a DATE object. 您可以做一些非常难看的事情,使用STR_TO_DATE()将其转换为日期,然后进行比较,但我建议您重新设置表结构以将此日期存储为DATE对象。

SELECT * FROM table WHERE ... AND 
    STR_TO_DATE(date, '%m/%d/%Y') BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
    OR date ISNULL

This assumes that $date is in the correct MySQL DATE format of 2012-03-14 (for 14th March 2012) 这假设$date格式为2012-03-14的正确MySQL DATE格式(2012年3月14日)

You need to enclose the entire date checking part in brackets, like so: 您需要将整个日期检查部分括在括号中,如下所示:

$sql = mysql_query(" SELECT * FROM table WHERE ... AND 
       (
       date BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
       OR date ISNULL
       )");

And it should work as expected. 它应该按预期工作。

Apart from the missing brackets, your condition seems to take into account NULLs all right (ie NULLs would be included in the output regardless of the argument passed). 除了缺少括号之外,您的条件似乎都考虑了NULL(即无论传递的参数如何,NULL都将包含在输出中)。

UPDATE UPDATE

It seems I was mistaken in presuming that the date column was the date type. 似乎我错误地认为date列是date类型。 @James C has got the point , as well as @nnichols , and their suggestion about changing the column type seems the way to go in your situation. @詹姆斯C已得到了点以及@nnichols ,和他们有关更改列类型建议似乎在你的情况的路要走。

You have to CAST/CONVERT your date field that you are getting from MySQL (you really called a date field 'date'? that's a bad idea). 你必须CAST / CONVERT你从MySQL获得的日期字段(你真的称为日期字段'日期'?这是一个坏主意)。

The best answer is to convert your date column to an actual DATE type. 最佳答案是将日期列转换为实际的DATE类型。 You should also not really use the mm/dd/yyyy format as its ambiguous. 您也不应该使用mm / dd / yyyy格式作为其模糊不清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM