gpt4 book ai didi

mysql - 如何选择比一周新的行?

转载 作者:行者123 更新时间:2023-11-29 05:56:26 25 4
gpt4 key购买 nike

使用 MariaDB 10,我想查询过去一周文章的 article 表:

这是我的查询:

SELECT * FROM article WHERE category="News" AND created_at < NOW() - INTERVAL 1 WEEK ORDER BY created_at DESC;

但它会返回所有文章。

explain article ;

+-------------+-----------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------------+------+-----+-------------------+----------------+
| id | int(6) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(150) | NO | | NULL | |
| content | mediumtext | NO | | NULL | |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | |
| category | varchar(64) | NO | | test | |

我怎样才能做到这一点?

最佳答案

逻辑是倒退的。你要>不是 < :

SELECT a.*
FROM article a
WHERE category = 'News' AND
created_at > NOW() - INTERVAL 1 WEEK
ORDER BY created_at DESC;

为了提高性能,您需要在 article(category, created_at) 上建立索引.

关于mysql - 如何选择比一周新的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49074785/

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