当前位置:网站首页>MySQL: merge query results and aliases

MySQL: merge query results and aliases

2022-06-09 07:29:00 Cat fearless mouse a

Merge query results

1、 utilize UNION keyword , You can give more than one SELECT sentence , And combine their query results into a single result set

2、 When merging , Number of columns corresponding to two query results 、 The data type must be the same

3、 each SELECT Use... Between statements UNION or UNION ALL Keyword separation
    ⑴UNION Don't use keywords ALL, Duplicate records will be deleted during execution , All returned rows are unique
    ⑵ Use keywords ALL The function of is not to delete duplicate rows or automatically sort the results

4、 grammar :SELECT column,... FROM table1 UNION [ALL] SELECT column,... FROM table2

example 1:UNION

mysql> SELECT * FROM fruits WHERE f_price >= 120;
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
|    2 |  102 | apple  | 120     |
|    3 |  103 | melon  | 130     |
|    5 |  103 | grape  | 150     |
+------+------+--------+---------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM fruits WHERE f_price IN (111,120);
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
|    1 |  101 | orange | 111     |
|    2 |  102 | apple  | 120     |
+------+------+--------+---------+
2 rows in set (0.00 sec)

--  Use UNION: Delete duplicate lines 
mysql> SELECT * FROM fruits WHERE f_price >= 120 UNION SELECT * FROM fruits WHERE f_price IN (111,120);
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
|    2 |  102 | apple  | 120     |
|    3 |  103 | melon  | 130     |
|    5 |  103 | grape  | 150     |
|    1 |  101 | orange | 111     |
+------+------+--------+---------+
4 rows in set (0.00 sec)


example 2:

--  Use UNION ALL: Do not delete duplicate rows 
mysql> SELECT * FROM fruits WHERE f_price >= 120 UNION ALL SELECT * FROM fruits WHERE f_price IN (111,120);
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
|    2 |  102 | apple  | 120     |
|    3 |  103 | melon  | 130     |
|    5 |  103 | grape  | 150     |
|    1 |  101 | orange | 111     |
|    2 |  102 | apple  | 120     |
+------+------+--------+---------+
5 rows in set (0.00 sec)

 

Alias the table

1、 When the name of the table is very long or when some special query is performed , For ease of operation or when you need to use the same table multiple times , You can specify an alias for the table , Substitute for the original name

2、 When aliasing a table , Be sure not to conflict with the names of other tables in the database

3、 The syntax is as follows :

     Table name  [AS]  Table alias 
    
     Parameter description 
         Table name : Name of the data table stored in the database 
         Table alias : New name of the table specified for query 
        AS: by Mysql keyword , Is an optional parameter 
        

example 2: Do not omit keywords AS

mysql> SELECT * FROM fruits AS f;
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
|    1 |  101 | orange     | 111     |
|    2 |  102 | apple      | 120     |
|    3 |  103 | melon      | 130     |
|    4 |  101 | banana     | 101     |
|    5 |  103 | grape      | 150     |
|    6 |  101 | huolongguo | 100     |
+------+------+------------+---------+
6 rows in set (0.00 sec)

notes :
1、 In the example above , Give table fruits Took an alias as f, So behind SQL Use... In the statement fruits You can use its alias when using a table f Instead of

2、 Specify an alias for the table ,AS Keywords can be omitted

example 3: Omit AS keyword

--  Using the fields in the table ( Column ) when , Best use " Table alias . Name " To indicate which table the column belongs to 
--  If the table does not have an alias , That can be used " Table name . Name " To show , But this is a bit of a hassle , This is one of the benefits of aliases 
mysql> SELECT f.f_id,f.s_id,f.f_name,f.f_price FROM fruits f  WHERE f.f_price >= 120;
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
|    2 |  102 | apple  | 120     |
|    3 |  103 | melon  | 130     |
|    5 |  103 | grape  | 150     |
+------+------+--------+---------+
3 rows in set (0.00 sec)

example 4: Alias multiple tables

mysql> SELECT * FROM fruits;
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
|    1 |  101 | orange     | 111     |
|    2 |  102 | apple      | 120     |
|    3 |  103 | melon      | 130     |
|    4 |  101 | banana     | 101     |
|    5 |  103 | grape      | 150     |
|    6 |  101 | huolongguo | 100     |
+------+------+------------+---------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM suppliers;
+------+----------------+--------+-------------+
| s_id | s_name         | s_city | s_telephone |
+------+----------------+--------+-------------+
|  101 |  The first fruit merchant in Southwest China  |  Chengdu    | 9090980     |
|  102 |  The first fruit merchant in East China  |  Shandong    | 12345678911 |
|  103 |  Northwest... Northwest        |  shanxi    | 98765432101 |
+------+----------------+--------+-------------+
3 rows in set (0.00 sec)

--  Alias multiple tables in a join query 
mysql> SELECT s.s_id,s.s_name,s.s_name,s.s_telephone,f.f_name FROM suppliers s LEFT JOIN fruits f ON f.s_id=s.s_id WHERE f.f_price >= 120;
+------+----------------+----------------+-------------+--------+
| s_id | s_name         | s_name         | s_telephone | f_name |
+------+----------------+----------------+-------------+--------+
|  102 |  The first fruit merchant in East China  |  The first fruit merchant in East China  | 12345678911 | apple  |
|  103 |  Northwest... Northwest        |  Northwest... Northwest        | 98765432101 | melon  |
|  103 |  Northwest... Northwest        |  Northwest... Northwest        | 98765432101 | grape  |
+------+----------------+----------------+-------------+--------+
3 rows in set (0.00 sec)

example 5: Alias the same table differently

mysql> SELECT * FROM fruits f1,fruits f2 WHERE f1.s_id=f2.s_id;
+------+------+------------+---------+------+------+------------+---------+
| f_id | s_id | f_name     | f_price | f_id | s_id | f_name     | f_price |
+------+------+------------+---------+------+------+------------+---------+
|    1 |  101 | orange     | 111     |    1 |  101 | orange     | 111     |
|    4 |  101 | banana     | 101     |    1 |  101 | orange     | 111     |
|    6 |  101 | huolongguo | 100     |    1 |  101 | orange     | 111     |
|    2 |  102 | apple      | 120     |    2 |  102 | apple      | 120     |
|    3 |  103 | melon      | 130     |    3 |  103 | melon      | 130     |
|    5 |  103 | grape      | 150     |    3 |  103 | melon      | 130     |
|    1 |  101 | orange     | 111     |    4 |  101 | banana     | 101     |
|    4 |  101 | banana     | 101     |    4 |  101 | banana     | 101     |
|    6 |  101 | huolongguo | 100     |    4 |  101 | banana     | 101     |
|    3 |  103 | melon      | 130     |    5 |  103 | grape      | 150     |
|    5 |  103 | grape      | 150     |    5 |  103 | grape      | 150     |
|    1 |  101 | orange     | 111     |    6 |  101 | huolongguo | 100     |
|    4 |  101 | banana     | 101     |    6 |  101 | huolongguo | 100     |
|    6 |  101 | huolongguo | 100     |    6 |  101 | huolongguo | 100     |
+------+------+------------+---------+------+------+------------+---------+
14 rows in set (0.00 sec)

notes :
1、 Two tables connected in a join query can be the same table , At this point, you need to give different aliases to the same table to distinguish them from each other

2、 In this case , If you don't use table aliases ,Mysql I don't know which one I'm referring to fruits surface . This is a very useful place for other names

3、 One SQL When multiple tables are involved in a statement , It is best to alias each table , Using table fields ( Column ) when , Use " Table alias . Name " The way , To distinguish which table the column belongs to

 

Alias the field

1、SELECT Statement to display query results ,MySQL It will show each SELECT The output column specified later , In some cases , The name of the displayed column may be too long or the name may not be intuitive , In this case, you can specify the column alias

2、Mysql You can specify column aliases , Replace field or expression

3、 The syntax is as follows ,

     Name  [AS]  Column alias 
    
     Parameter description :
     Name : The name of the field in the table 
     Column alias : New name for the field 
    AS Keyword is an optional parameter 

example 6:

--  When the column name is not aliased , The default is the actual column name f_name and f_price
mysql> SELECT f1.f_name, f1.f_price FROM fruits AS f1 WHERE  f1.f_price<130;
+------------+---------+
| f_name     | f_price |
+------------+---------+
| orange     | 111     |
| apple      | 120     |
| banana     | 101     |
| huolongguo | 100     |
+------------+---------+
4 rows in set (0.00 sec)

--  Alias the column name , The alias is returned fruit_name and fruit_price
mysql> SELECT f1.f_name AS fruit_name, f1.f_price AS fruit_price FROM fruits AS f1 WHERE  f1.f_price<130;
+------------+-------------+
| fruit_name | fruit_price |
+------------+-------------+
| orange     | 111         |
| apple      | 120         |
| banana     | 101         |
| huolongguo | 100         |
+------------+-------------+
4 rows in set (0.00 sec)

example 7: Aliasing aggregate function values

mysql> SELECT COUNT(*) FROM fruits AS f1 WHERE  f1.f_price<130;
+----------+
| COUNT(*) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)

mysql> SELECT COUNT(*) AS total FROM fruits AS f1 WHERE  f1.f_price<130;
+-------+
| total |
+-------+
|     4 |
+-------+
1 row in set (0.00 sec)

notes :
1、 Table aliases are only used when executing queries , Does not show in the returned results ; After the column alias is defined , Will return to the client to display , The result field displayed is the alias of the field column

2、 Assign an alias to the column name ,AS Keywords can be omitted

example 8:

mysql> SELECT f1.f_name fruit_name, f1.f_price fruit_price FROM fruits f1 WHERE  f1.f_price<130;
+------------+-------------+
| fruit_name | fruit_price |
+------------+-------------+
| orange     | 111         |
| apple      | 120         |
| banana     | 101         |
| huolongguo | 100         |
+------------+-------------+
4 rows in set (0.00 sec)

notes : When aliasing tables or column names , If you omit AS keyword , Then you need to use spaces to separate

原网站

版权声明
本文为[Cat fearless mouse a]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021422096218.html