The MySQL BETWEEN
operator is used to select values within a range of values. It is commonly used in the WHERE
clause of a SELECT
statement to filter results based on a range of values.
The syntax of the BETWEEN
operator is as follows:
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
In this syntax, column_name
is the name of the column you want to check, table_name
is the name of the table, value1
is the lower boundary of the range, and value2
is the upper boundary of the range.
Here’s an example of how you could use the BETWEEN
operator to select data from a table:
SELECT * FROM orders WHERE order_date BETWEEN '2022-01-01' AND '2022-03-31';
In this example, the BETWEEN
operator is used to select all orders placed between January 1st, 2022 and March 31st, 2022.
Note that the BETWEEN
operator is inclusive, which means that it will include the lower and upper boundaries in the results. For example, the query above will include orders placed on March 31st, 2022. If you want to exclude the upper or lower boundary, you can use the >
or <
operator instead.
Also, note that the BETWEEN
operator can be used with various data types such as numeric, date, time, and datetime.