当我在搜索时尝试选择特定月份和年份的记录时,我找到了半个解决方案
mysql: select all entries from a particular month using PHP
select *
from thetable
where monthname(date_field) = 'February'
然后我按照
修改它select *
from payments
where monthname(date) = 'February' and EXTRACT(date )='2011';
但它显示错误选择mounth和year的正确方法
在我的表格中,我将日期存储为2011-2-4
答案 0 :(得分:1)
您的专栏似乎不属于Date
数据类型,因此您可以做的最佳方法是使用DATE
将其转换为STR_TO_DATE
SELECT *
FROM Payments
WHERE MONTHNAME(STR_TO_DATE(date,'%Y-%c-%e')) = 'FEBRUARY' AND
YEAR(STR_TO_DATE(date,'%Y-%c-%e')) = 2011
答案 1 :(得分:1)
试试这个:
select * from payments p
where year(p.date) = 2011 and monthname(p.date) = 'February'