![](/img/trans.png)
[英]How to select records between two dates using only month and year in PHP and MySQL?
[英]select data from two table using month and year only in mysql
Entered-month Entered-year Amount
-------------------------------------
01 2013 3000
02 2013 4000
. . .
. . .
. . .
01 2014 2000
我的表是这样的,我必须在两个日期之间获取数据
从2011年11月11日至2014年3月1日
使用MySQL函数STR_TO_DATE
和CONCAT
将您的月和年字段放在一起作为日期。 http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date http://dev.mysql.com/doc/refman/5.1/en /string-functions.html#function_concat
select Amount
from tbl1
where STR_TO_DATE(CONCAT(tbl1.Entered-month,'-',tbl1.Entered-year),'%m-%Y') >= [start date]
and STR_TO_DATE(CONCAT(tbl1.Entered-month,'-',tbl1.Entered-year),'%m-%Y') <= [end date]
要么
select Amount
from tbl1
where STR_TO_DATE(CONCAT(tbl1.Entered-month,'-',tbl1.Entered-year),'%m-%Y')
between [start date] and [end date]
传递给查询时,请确保[start date]
和[end date]
采用正确的日期格式。 如果这些参数是字符串参数,请对这些参数使用STR_TO_DATE
。
select Amount
from tbl1
where STR_TO_DATE(CONCAT(tbl1.Entered-month,'-',tbl1.Entered-year),'%m-%Y')
between STR_TO_DATE(startDate,'%Y-%m-%d') and STR_TO_DATE(endDate,'%Y-%m-%d')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.