如何从“2012-02”开始选择mysql中的所有字段?
我尝试了什么:
mysql> SELECT title,id FROM dle_post WHERE approve='1' AND date = '2012-02-13 21:35:00';
+-------------------------------------------------+----+
| title | id |
+-------------------------------------------------+----+
| test title | 7 |
+-------------------------------------------------+----+
mysql> SELECT title,id FROM dle_post WHERE approve='1' AND date='2012-02%';
Empty set, 2 warnings (0.00 sec)
mysql> SHOW WARNINGS;
+---------+------+-----------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------------------+
| Warning | 1292 | Incorrect datetime value: '2012-02%' for column 'date' at row 1 |
| Warning | 1292 | Incorrect datetime value: '2012-02%' for column 'date' at row 1 |
+---------+------+-----------------------------------------------------------------+
答案 0 :(得分:2)
理想情况下,日期应视为日期,而不是字符串。此查询应返回2012年2月的所有行,并且应该优于LIKE %
个查询:
SELECT title, id
FROM dle_post
WHERE approve = 1
AND date >= '2012-02-01'
AND date < '2012-02-01' + INTERVAL 1 MONTH
答案 1 :(得分:0)
更改为
SELECT title,id FROM dle_post WHERE approve='1' AND date LIKE '2012-02%';
答案 2 :(得分:0)
使用like运算符检查这些值
SELECT title,id FROM dle_post WHERE approve='1' AND date LIKE '2012-02%';
答案 3 :(得分:0)
你试过吗
date LIKE '2012-02%'
而不是
date='2012-02%'
您可能还需要使用DATE_FORMAT(日期,格式)将日期转换为字符串
请参阅:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format