首頁  >  文章  >  資料庫  >  如何在MySQL表中按日期搜尋記錄?

如何在MySQL表中按日期搜尋記錄?

WBOY
WBOY轉載
2023-09-09 08:53:13566瀏覽

如何在MySQL表中按日期搜尋記錄?

假設我們有一個表“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表中按日期搜尋記錄?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除