[英]Mysql Get table result between two dates
I know my question is similar to other question already answered but my issue is different because I need some alternative or advice no how to go the other way around. 我知道我的问题与已经回答的其他问题相似,但是我的问题有所不同,因为我需要一些替代方案或建议,而无需其他方法。
My issue is: I want to get values between either two dates or one according to what user wants.. 我的问题是:我想根据用户的需要在两个日期或一个日期之间获取值。
When User request data of one day.. php query data successful.. but problem is when data requested is between two dates.. 当用户请求一天的数据.. php查询数据成功..但问题是当请求的数据在两个日期之间时。
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '$first_date' AND
`meta_transaction_date` <= '$second_date' ");
return $query->result();
I get an empty set... 我得到一个空集...
So I thought may be the values are not submitted correct.. so I echoed the values to see if they are correct or not. 所以我认为可能是这些值未正确提交。因此我回显了这些值以查看它们是否正确。 I find they are correct...
我发现它们是正确的...
$first_date = 09/13/2014;
$ first_date = 09/13/2014;
$second_date = 09/19/2014;$ second_date = 2014年9月19日;
But if I try to put the value like 但是如果我尝试把价值
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '09/13/2014' AND
`meta_transaction_date` <= '09/19/2014' ");
return $query->result();
I get my result back correct.. so is there anything am doing it wrong?? 我得到的结果是正确的..所以有什么地方做错了吗?
MySQL有一个称为Between的内置函数,您可以像这样使用:
SELECT * FROM table_name WHERE date_column BETWEEN 'start_date_parameter' AND 'end_time_parameter'
Change the type of meta_transaction_date
to DATE
as that is what it is! 就是这样,将
meta_transaction_date
的类型更改为DATE
! Also use the standard 'yyyy-mm-dd'
when passing in DATE
s. 在传递
DATE
时,也请使用标准的'yyyy-mm-dd'
。
Your problem probably stems from string ordering of the 'mm/dd/yyyy'
US date format which is horrible for coding. 您的问题可能源于美国日期格式
'mm/dd/yyyy'
字符串排序,这对于编码来说很糟糕。 If you wish to display the DATE
in this format, convert it when SELECT
ing the final output. 如果希望以这种格式显示
DATE
,请在SELECT
最终输出时进行转换。
Try to cast the date first , and then with between
statement: 尝试先投射日期,然后使用
between
语句:
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` BETWEEN
date_format(str_to_date('$first_date', '%d/%m/%Y'), '%Y-%m-%d') AND
date_format(str_to_date('$second_date', '%d/%m/%Y'), '%Y-%m-%d')");
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '09/13/2014'
AND `meta_transaction_date` <= '09/19/2014' ");
Since the above seems to be working fine, the problem is in your code. 由于上面的方法似乎工作正常,因此问题出在您的代码中。
$query = $this->db->query("SELECT `meta_transaction_date` FROM meta_receipt_data WHERE
meta_transaction_date BETWEEN "
.date( "Y-M-d", ( strtotime($first_date) ) )." AND "
.date( "Y-M-d", ( strtotime($second_date) ) ) );
A word of advice, do not use queries like SELECT *
as they will degrade performance of your application. 请注意,请勿使用
SELECT *
类的查询,因为它们会降低应用程序的性能。 And I just read in your comment to your own question: 我只是读了您对自己的问题的评论:
I have set the type as Varchar
我已将类型设置为Varchar
Do not do that. 不要那样做。 It is best to use the DATE type to store dates.
最好使用DATE类型存储日期。 You can use the query:
您可以使用查询:
ALTER TABLE `meta_receipt_data`
MODIFY `meta_transaction_date` DATE NOT NULL;`
Well, that is assuming you wish to keep the column to not accept null values. 好吧,这是假设您希望保留该列不接受空值。
I found that the value had space before and after so I use $first = trim($first_date);
我发现该值前后都有空格,因此我使用
$first = trim($first_date);
and problem solved... 问题解决了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.