我怎样才能 select 行比一周新?

How can I select rows newer than a week?

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

这是我的查询:

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

但它 returns 所有文章都代替了。

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) 上建立索引。