Methods for specifying date intervals in SQL include: using BETWEEN and AND: BETWEEN 'start date' AND 'end date' using >= and <=: 'start date' >= 'end Date' using >= and <= and <: 'Start Date' >= 'End Date', exclude end date
# #Using date intervals in SQL
1. Using BETWEEN and AND
Both BETWEEN and AND operators can be used to specify date intervals. The syntax of BETWEEN is:<code class="sql">BETWEEN <起始日期> AND <结束日期></code>The syntax of AND is:
<code class="sql"><起始日期> AND <结束日期></code>The two operators have the same effect.
2. Use >= and <=
= and <= operators can also be used to specify a date range. The syntax is:<code class="sql"><起始日期> >= <结束日期></code><code class="sql"><结束日期> <= <起始日期></code>
3. Use >= and <
= and < operators can also be used to specify a date range, but it should be noted that , which excludes the end date. The syntax is:<code class="sql"><起始日期> >= <结束日期></code>
4. Usage example
Query all orders between January 1, 2023 and March 31, 2023:<code class="sql">SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-03-31';</code>Query all orders before January 1, 2023 or after March 31, 2023:
<code class="sql">SELECT * FROM orders WHERE order_date < '2023-01-01' OR order_date > '2023-03-31';</code>
The above is the detailed content of How to use date range in sql. For more information, please follow other related articles on the PHP Chinese website!