gpt4 book ai didi

mysql:选择以 2012-02* 开头的所有字段

转载 作者:行者123 更新时间:2023-11-29 08:40:51 26 4
gpt4 key购买 nike

如何选择mysql中以“2012-02”开头的所有字段?

我尝试过的:

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 |
+---------+------+-----------------------------------------------------------------+

最佳答案

理想情况下,日期应被视为日期,而不是字符串。此查询应返回 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

关于mysql:选择以 2012-02* 开头的所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13844877/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com