我试图让所有项目'来自下表的ID,条件为uid =' 1'和月份='八月'和年=' 2016'
date | uid | project
-------------------------------------------
Wed, 01 Aug 2016 | 1 | 11
Tue, 15 Aug 2016 | 2 | 11
Mon, 31 Aug 2016 | 1 | 11
Thu, 05 Sep 2016 | 1 | 12
Sat, 10 Sep 2016 | 2 | 12
Mon, 12 Feb 2017 | 3 | 12
Tue, 31 Feb 2017 | 1 | 13
Wed, 31 Mar 2017 | 2 | 13
Fri, 31 Mar 2017 | 3 | 13
Thu, 31 Mar 2017 | 1 | 14
以下是我使用的查询
SELECT project FROM logs where uid="1" AND MONTHNAME(STR_TO_DATE(date,'%W, %d %M %Y'))="August" AND YEAR(STR_TO_DATE(date,'%W, %d %M %Y'))="2016";
但它也正在返回其他月份的记录。我在这做错了什么?
答案 0 :(得分:2)
我只会使用like
:
SELECT project
FROM logs
WHERE uid = 1 AND
date LIKE '% Aug 2016';
不要将日期/时间值存储为字符串。如果你这样做,不要打电话给他们"约会",称他们为" datestr"防止意外错误。但如果它是一个字符串,你可以像对待字符串一样对待它。
答案 1 :(得分:0)
SELECT project
FROM logs
WHERE uid= 1
AND MONTHNAME(STR_TO_DATE(date,'%W, %d %M %Y'))= 'August'
AND YEAR(STR_TO_DATE(date,'%W, %d %M %Y'))= 2016
;