SQL zero-based introductory learning (16)
SQL function
EXISTS operator
The EXISTS operator is used to determine whether the query clause has records, and returns True if one or more records exist, otherwise returns False.
SQL EXISTS syntax
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| 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/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
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)
SQL EXISTS instance
Now we want to find the existence of a website with total visits (count field) greater than 200.
We use the following SQL statement:
SELECT Websites.name, Websites.url
FROM Websites
WHERE EXISTS (SELECT count FROM access_log WHERE Websites.id = access_log.site_id AND count > 200);
The output result of executing the above SQL is as follows:
EXISTS can be used together with NOT to find records that do not match the query statement:
SELECT Websites.name, Websites.url
FROM Websites
WHERE NOT EXISTS (SELECT count FROM access_log WHERE Websites.id = access_log.site_id AND count > 200);
Executing the above SQL output results are as follows:
UCASE() function
The UCASE() function converts the value of a field to uppercase.
SQL UCASE() syntax
SELECT UCASE(column_name) FROM table_name;
Syntax for SQL Server
SELECT UPPER(column_name) FROM table_name;
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| 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/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
SQL UCASE() instance
The following SQL statement selects the "name" and "url" columns from the "Websites" table and converts the value of the "name" column to uppercase:
SELECT UCASE(name) AS site_title, url
FROM Websites;
Executing the above SQL output results are as follows:
LCASE() function
The LCASE() function converts the value of a field to lowercase.
SQL LCASE() syntax
SELECT LCASE(column_name) FROM table_name;
用于 SQL Server 的语法
SELECT LOWER(column_name) FROM table_name;
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| 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/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
SQL LCASE() instance
The following SQL statement selects the "name" and "url" columns from the "Websites" table and converts the value of the "name" column to lowercase:
SELECT LCASE(name) AS site_title, url
FROM Websites;
Executing the above SQL output results are as follows:
MID() function
The MID() function is used to extract characters from a text field.
SQL MID() syntax
SELECT MID(column_name,start[,length]) FROM table_name;
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| 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/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
SQL MID() instance
The following SQL statement extracts the first 4 characters from the "name" column of the "Websites" table:
SELECT MID(name,1,4) AS ShortTitle
FROM Websites;
Executing the above SQL output results are as follows:
LEN() function
The LEN() function returns the length of the value in a text field.
SQL LEN() syntax
SELECT LEN(column_name) FROM table_name;
The function in MySQL is LENGTH():
SELECT LENGTH(column_name) FROM table_name;
demo database
In this tutorial, we will use the RUNOOB sample database.
Here is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| 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/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
SQL LEN() instance
The following SQL statement selects the length of the values in the "name" and "url" columns from the "Websites" table:
SELECT name, LENGTH(url) as LengthOfURL
FROM Websites;
Executing the above SQL output results are as follows: