SQL zero-based introductory learning (7)

SQL zero-based introductory learning (6)

SQL BETWEEN operator

The BETWEEN operator selects values ​​within the data range between two values. These values ​​can be numbers, text, or dates.

SQL BETWEEN syntax

SELECT column1, column2, ...
FROM table_name
WHERE column BETWEEN value1 AND value2;

Parameter description:
column1, column2, …: the name of the field to be selected, which can be multiple fields. If no field names are specified, all fields will be selected.
table_name: The name of the table to be queried.
column: The name of the field to be queried.
value1: The starting value of the range.
value2: The end value of the range.

demo database

In this tutorial, we will use the RUNOOB sample database.

Here is the data from the "Websites" table:

mysql> SELECT * FROM Websites;
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 | 菜鸟教程       | http://www.runoob.com/    |  5000 | USA     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

Example of the BETWEEN operator

The following SQL statement selects all websites where alexa is between 1 and 20:

SELECT * FROM Websites
WHERE alexa BETWEEN 1 AND 20;

Execution output:
insert image description here

NOT BETWEEN operator instance

To display sites that are not within the scope of the example above, use NOT BETWEEN:

SELECT * FROM Websites
WHERE alexa NOT BETWEEN 1 AND 20;

Execution output:
insert image description here

BETWEEN operator instance with IN

The following SQL statement selects all websites where alexa is between 1 and 20 but country is not USA and IND:

SELECT * FROM Websites
WHERE (alexa BETWEEN 1 AND 20)
AND country NOT IN ('USA', 'IND');

Execution output:
insert image description here

BETWEEN operator instance with literal values

The following SQL statement selects all sites whose name starts with a letter between 'A' and 'H':

SELECT * FROM Websites
WHERE name BETWEEN 'A' AND 'H';

Execution output:
insert image description here

NOT BETWEEN operator instance with literal values

The following SQL statement selects all sites whose name does not start with a letter between 'A' and 'H':

SELECT * FROM Websites
WHERE name NOT BETWEEN 'A' AND 'H';

Execution output:
insert image description here

example table

The following is the data of the "access_log" website access record table, among which:
aid: auto-increment id.
site_id: the website id corresponding to the websites table.
count: number of visits.
date: It is the access date.

mysql> SELECT * FROM access_log;
+-----+---------+-------+------------+
| aid | site_id | count | date       |
+-----+---------+-------+------------+
|   1 |       1 |    45 | 2016-05-10 |
|   2 |       3 |   100 | 2016-05-13 |
|   3 |       1 |   230 | 2016-05-14 |
|   4 |       2 |    10 | 2016-05-14 |
|   5 |       5 |   205 | 2016-05-14 |
|   6 |       4 |    13 | 2016-05-15 |
|   7 |       3 |   220 | 2016-05-15 |
|   8 |       5 |   545 | 2016-05-16 |
|   9 |       3 |   201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)

The access_log table SQL file used in this tutorial: access_log.sql.

BETWEEN operator instance with date values

The following SQL statement selects all access records whose date is between '2016-05-10' and '2016-05-14':

SELECT * FROM access_log
WHERE date BETWEEN '2016-05-10' AND '2016-05-14';

Execution output:
insert image description here

Note that the BETWEEN operator produces different results in different databases!
In some databases, BETWEEN selects fields between but not including the two test values.
In some databases, BETWEEN selects fields between and including the two test values.
In some databases, BETWEEN selects fields between two values ​​up to and including the first test value but excluding the last test value.

So check how your database handles the BETWEEN operator!

SQL aliases

By using SQL, you can specify aliases for table names or column names.

Basically, aliases are created to make column names more readable.

SQL alias syntax for columns

SELECT column_name AS alias_name
FROM table_name;
表的 SQL 别名语法
SELECT column_name(s)
FROM table_name AS alias_name;

demo database

In this tutorial, we will use the RUNOOB sample database.

Here is the data from the "Websites" table:

mysql> SELECT * FROM Websites;
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 | 菜鸟教程       | http://www.runoob.com/    |  5000 | USA     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

The following is the data of the "access_log" website access record table:

mysql> SELECT * FROM access_log;
+-----+---------+-------+------------+
| aid | site_id | count | date       |
+-----+---------+-------+------------+
|   1 |       1 |    45 | 2016-05-10 |
|   2 |       3 |   100 | 2016-05-13 |
|   3 |       1 |   230 | 2016-05-14 |
|   4 |       2 |    10 | 2016-05-14 |
|   5 |       5 |   205 | 2016-05-14 |
|   6 |       4 |    13 | 2016-05-15 |
|   7 |       3 |   220 | 2016-05-15 |
|   8 |       5 |   545 | 2016-05-16 |
|   9 |       3 |   201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)

Column alias instance

The following SQL statement specifies two aliases, one for the name column and one for the country column. Tip: If column names contain spaces, double quotes or square brackets are required:

SELECT name AS n, country AS c
FROM Websites;

Execution output:
insert image description here
In the following SQL statement, we combine the three columns (url, alexa, and country) and create an alias called "site_info":

SELECT name, CONCAT(url, ', ', alexa, ', ', country) AS site_info
FROM Websites;

Execution output:
insert image description here

table alias instance

The following SQL statement selects all the access records of "Rookie Tutorial". We use the "Websites" and "access_log" tables, and give them table aliases "w" and "a" respectively (using aliases makes the SQL shorter):

SELECT w.name, w.url, a.count, a.date
FROM Websites AS w, access_log AS a
WHERE a.site_id=w.id and w.name="菜鸟教程";

Execution output:
insert image description here

The same SQL statement without the alias:

SELECT Websites.name, Websites.url, access_log.count, access_log.date
FROM Websites, access_log
WHERE Websites.id=access_log.site_id and Websites.name="菜鸟教程";

Execute output results:
insert image description here
Using aliases is useful in the following cases:
1. More than one table is involved in the query
2. Functions are used in the query
3. Column names are long or poorly readable
4. Two columns or Combine multiple columns together

Guess you like

Origin blog.csdn.net/weixin_44006731/article/details/129176926