MySQL BETWEEN Clause
MySQL BETWEEN Clause
You can use BETWEEN clause to replace a combination of "greater than equal AND less than equal" conditions. To understand BETWEEN clause, consider an employee_tbl table, which is having the following records:
mysql > SELECT * FROM employee_tbl ; +------+------+------------+--------------------+ | id
| name | work_date | daily_typing_pages | +------+------+------------+--------------------+ |
1 | John | 2007 - 01 - 24 |
2 | Ram | 2007 - 05 - 27 |
3 | Jack | 2007 - 05 - 06 |
3 | Jack | 2007 - 04 - 06 |
4 | Jill | 2007 - 04 - 06 |
5 | Zara | 2007 - 06 - 06 |
5 | Zara | 2007 - 02 - 06 |
+------+------+------------+--------------------+
7 rows in set ( 0.00 sec )
Now, suppose based on the above table you want to fetch records with conditions daily_typing_pages more than 170 and equal and less than 300 and equal. This can be done using >= and <= conditions as follows:
mysql > SELECT * FROM employee_tbl
-> WHERE daily_typing_pages >= 170 AND -> daily_typing_pages <= 300 ;
+------+------+------------+--------------------+ | id
| name | work_date | daily_typing_pages | +------+------+------------+--------------------+
1 | John | 2007 - 01 - 24 |
2 | Ram | 2007 - 05 - 27 |
3 | Jack | 2007 - 05 - 06 |
4 | Jill | 2007 - 04 - 06 |
5 | Zara | 2007 - 06 - 06 |
+------+------+------------+--------------------+
5 rows in set ( 0.03 sec )
Same can be achieved using BETWEEN clause as follows: mysql > SELECT * FROM employee_tbl
-> WHERE daily_typing_pages BETWEEN 170 AND 300 ; +------+------+------------+--------------------+ | id
| name | work_date | daily_typing_pages | +------+------+------------+--------------------+ |
1 | John | 2007 - 01 - 24 |
2 | Ram | 2007 - 05 - 27 |
3 | Jack | 2007 - 05 - 06 |
4 | Jill | 2007 - 04 - 06 |
5 | Zara | 2007 - 06 - 06 |
+------+------+------------+--------------------+
5 rows in set ( 0.03 sec )
MySQL UNION Keyword
You can use UNION if you want to select rows one after the other from several tables or several sets of rows from
a single table all as a single result set. UNION is available as of MySQL 4.0. This section illustrates how to use it.
Suppose you have two tables that list prospective and actual customers, a third that lists vendors from whom you purchase supplies, and you want to create a single mailing list by merging names and addresses from all three tables. UNION provides a way to do this. Assume the three tables have the following contents:
mysql > SELECT * FROM prospect ; +---------+-------+------------------------+ | fname | lname | addr
+---------+-------+------------------------+
| Peter | Jones | 482 Rush St ., Apt . 402 |
| Bernice | Smith | 916 Maple Dr .
+---------+-------+------------------------+ mysql > SELECT * FROM customer ; +-----------+------------+---------------------+ | last_name | first_name | address
+-----------+------------+---------------------+ | Peterson | Grace
| 16055 Seminole Ave . |
| Smith | Bernice
| 916 Maple Dr .
| Brown | Walter
| 8602 1st St .
+-----------+------------+---------------------+ mysql > SELECT * FROM vendor ; +-------------------+---------------------+ | company
| street
+-------------------+---------------------+
| ReddyParts , Inc . | 38 Industrial Blvd . | | Parts - to - go , Ltd . | 213B Commerce Park . |
+-------------------+---------------------+
It does not matter if all the three tables have different column names. The following query illustrates how to select names and addresses from the three tables all at once:
mysql > SELECT fname , lname , addr FROM prospect -> UNION -> SELECT first_name , last_name , address FROM customer -> UNION
-> SELECT company , '' , street FROM vendor ;
+-------------------+----------+------------------------+ | fname
| lname
| addr
+-------------------+----------+------------------------+ | Peter
| Jones
| 482 Rush St ., Apt . 402 |
| Bernice
| Smith
| 916 Maple Dr .
| Grace
| Peterson | 16055 Seminole Ave .
| Walter
| Brown
| 8602 1st St .
| ReddyParts , Inc . |
| 38 Industrial Blvd .
| Parts - to - go , Ltd . |
| 213B Commerce Park .
+-------------------+----------+------------------------+ If you want to select all records, including duplicates, follow the first UNION keyword with ALL: mysql > SELECT fname , lname , addr FROM prospect
-> UNION ALL -> SELECT first_name , last_name , address FROM customer -> UNION
-> SELECT company , '' , street FROM vendor ;
+-------------------+----------+------------------------+ | fname
| lname
| addr
+-------------------+----------+------------------------+ | Peter
| Jones
| 482 Rush St ., Apt . 402 |
| Bernice
| Smith
| 916 Maple Dr .
| Grace
| Peterson | 16055 Seminole Ave .
| Bernice
| Smith
| 916 Maple Dr .
| Walter
| Brown
| 8602 1st St .
| ReddyParts , Inc . |
| 38 Industrial Blvd .
| Parts - to - go , Ltd . |
| 213B Commerce Park .
+-------------------+----------+------------------------+