简体   繁体   中英

MySQL dateformat %c does not return results?

Why MySQL dateformat function does not fetch rows with a month in the query?

SELECT p.*  
FROM article AS p
WHERE p.parent_id = '6' 
AND DATE_FORMAT(p.backdated_on, '%Y %c') = '2015 8'  
GROUP BY p.article_id  
ORDER BY p.backdated_on DESC  
LIMIT 0, 6 

I have one row with the backdated_on of 2015-11-08 19:10:04

Any ideas why and how I can fix this?

The month is '2015 11':

mysql> select DATE_FORMAT('2015-11-08 19:10:04', '%Y %c');
+---------------------------------------------+
| DATE_FORMAT('2015-11-08 19:10:04', '%Y %c') |
+---------------------------------------------+
| 2015 11                                     |
+---------------------------------------------+
1 row in set (0.00 sec)

'2015 8' is matching date like 2015-08-dd ... .

If the date is 2015-11-08 19:10:04 , then the corresponding logic would be:

DATE_FORMAT(p.backdated_on, '%Y %c') = '2015 11'  

However, it is a bad habit to compare dates using string functions. Instead:

(p.backdated_on >= '2015-11-01' and p.backdated_on < '2015-12-01')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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