Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

MySQL BETWEEN


The MYSQL BETWEEN condition specifies how to retrieve values from an expression within a specific range. It is used with SELECT, INSERT, UPDATE and DELETE statement.

Syntax:

  1. expression BETWEEN value1 AND value2;

Parameters

expression: It specifies a column.

value1 and value2: These values define an inclusive range that expression is compared to.

Let’s take some examples:

(i) MySQL BETWEEN condition with numeric value:

Consider a table “officers” having the following data.

mysqlbetween1

Execute the following query:

  1. SELECT *
  2. FROM officers
  3. WHERE officer_id BETWEEN 1 AND 3;

Output:

mysqlbetween2

Note: In the above example, you can see that only three rows are returned between 1 and 3.

(ii) MySQL BETWEEN condition with date:

MySQL BETWEEN condition also facilitates you to retrieve records according to date.

See this example:

Consider a table “employees”, having the following data.

mysqlbetween3

Execute the following query:

  1. SELECT *
  2. FROM employees
  3. WHERE working_date BETWEEN CAST (‘2015-01-24’ AS DATEAND CAST (‘2015-01-25’ AS DATE);

Output:

mysqlbetween4

Note: In the above example you can see that only data between specific dates are shown.

Comments

comments