如何在 MySQL 表中按日期搜索记录?
mysqlmysqli database更新于 2025/3/28 18:20:17
假设我们有一个表"Order123",其中包含 ProductName、Quantity 和 OrderDate 列,如下所示 −
mysql> Select * from Order123; +-------------+----------+------------+ | ProductName | Quantity | OrderDate | +-------------+----------+------------+ | A | 100 | 2017-05-25 | | B | 105 | 2017-05-25 | | C | 55 | 2017-05-25 | | D | 250 | 2017-05-26 | | E | 500 | 2017-05-26 | | F | 500 | 2017-05-26 | | G | 500 | 2017-05-27 | | H | 1000 | 2017-05-27 | +-------------+----------+------------+ 8 rows in set (0.00 sec)
现在,如果我们想搜索某个特定日期(比如 2017 年 6 月 25 日)收到的订单数量,可以按如下方式进行 −
mysql> Select ProductName, Quantity from Order123 where OrderDate = '2017-05-25'; +-------------+----------+ | ProductName | Quantity | +-------------+----------+ | A | 100 | | B | 105 | | C | 55 | +-------------+----------+ 3 rows in set (0.00 sec)
假设我们已经存储了带有时间的 OrderDate,那么借助以下查询,我们可以获得指定的输出 −
mysql> Select ProductName, Quantity from Order123 where date(OrderDate) = '2017-05-25';
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

