MySQL entry: Data Manipulation

UNION:

Results MySQL UNION operator SELECT statement which is connected to two or more combinations of a result set. Multiple SELECT statement removes duplicate data.

SELECT culomns1, culomns2 FROM tables_name1 [WHERE conditions]
UNION [ALL | DISTINCT]
SELECT culomns1, culomns2 FROM tables_name2 [WHERE conditions];

DISTINCT: Optional, delete duplicate the results of the data set. By default, the UNION operator has removed the duplicate data, so the impact on the results DISTINCT modifier nothing.

ALL: Alternatively, return all the result sets that contain duplicate data.

Example:

These are two tables: websites and apps

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/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
| 7  | stackoverflow | http://stackoverflow.com/ |   0 | IND     |
+----+---------------+---------------------------+-------+---------+


mysql> SELECT * FROM apps;
+----+------------+-------------------------+---------+
| id | app_name   | url                     | country |
+----+------------+-------------------------+---------+
|  1 | QQ APP     | http://im.qq.com/       | CN      |
|  2 | 微博 APP | http://weibo.com/       | CN      |
|  3 | 淘宝 APP | https://www.taobao.com/ | CN      |
+----+------------+-------------------------+---------+
3 rows in set (0.00 sec)

Run the statement:

SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;

got the answer:

Run the statement :( with all)

SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;

got the answer:

ORDER BY Sort:

Statement:

SELECT columns1, columns2 from table_name1, table_name2
ORDER BY columns1, [columns2...] [ASC [DESC]]
  • Any field may be used as the sorting condition, thereby returning the query results sorted.
  • Setting a plurality of fields can be sorted.
  • ASC or DESC keyword can be used to set the search result in ascending or descending order. By default, it is ascending.
  • You can add WHERE ... LIKE clause to set conditions.

MySQL alphabetical order

If the character set used is gbk (Chinese character set encoding), added directly behind the ORDER BY in the query:

SELECT * FROM table_name
ORDER BY columns_name;

If the character set used is utf8 (Unicode), you need to be transcoded on the field and then sort:

SELECT * FROM table_name
ORDER BY CONVERT(columns_name using gbk);

GROUP BY grouping:

SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

Example:

There are the following table:

mysql> set names utf8;
mysql> SELECT * FROM employee_tbl;
+----+--------+---------------------+--------+
| id | name   | date                | singin |
+----+--------+---------------------+--------+
|  1 | 小明 | 2016-04-22 15:25:33 |      1 |
|  2 | 小王 | 2016-04-20 15:25:47 |      3 |
|  3 | 小丽 | 2016-04-19 15:26:02 |      2 |
|  4 | 小王 | 2016-04-07 15:26:14 |      4 |
|  5 | 小明 | 2016-04-11 15:26:40 |      4 |
|  6 | 小明 | 2016-04-04 15:26:54 |      2 |
+----+--------+---------------------+--------+
6 rows in set (0.00 sec)

The data tables are grouped by name with GROUP BY statement, and statistics for each person how many records:

mysql> SELECT name, COUNT(*) FROM   employee_tbl GROUP BY name;
+--------+----------+
| name   | COUNT(*) |
+--------+----------+
| 小丽 |        1 |
| 小明 |        3 |
| 小王 |        2 |
+--------+----------+
3 rows in set (0.01 sec)

Use WITH ROLLUP

WITH ROLLUP can be achieved then the same statistics (SUM, AVG, COUNT ...) on the basis of statistical data in the packet.

For example, we will be more data tables grouped by name, and then count the number of each person who is registered:

mysql> SELECT name, SUM(singin) as singin_count FROM  employee_tbl GROUP BY name WITH ROLLUP;
+--------+--------------+
| name   | singin_count |
+--------+--------------+
| 小丽 |            2 |
| 小明 |            7 |
| 小王 |            7 |
| NULL   |           16 |
+--------+--------------+

NULL indicates which records all logins.

You can use coalesce to set a NUll can replace the name, coalesce syntax:

select coalesce(a,b,c);

Parameters:! If a == null, B is selected; if b == null, C is selected; if a = null, then select a; if abc are null, then the return is null (no meaning).

The following examples if the name is empty we use instead of the total number of:

mysql> SELECT coalesce(name, '总数'), SUM(singin) as singin_count FROM  employee_tbl GROUP BY name WITH ROLLUP;
+--------------------------+--------------+
| coalesce(name, '总数') | singin_count |
+--------------------------+--------------+
| 小丽                   |            2 |
| 小明                   |            7 |
| 小王                   |            7 |
| 总数                   |           16 |
+--------------------------+--------------+

 

Guess you like

Origin blog.csdn.net/qq_26271435/article/details/89680729