MySQL query - aliasing tables and fields

In the content of grouping query, aggregate function query and nested subquery introduced earlier, the AS keyword is used in some places to specify a specific name for a column in the query result. When querying the inner join, specify two different names for the same table fruits. Here, you can take an alias for the field or table. When querying, use the alias to replace the specified content. The following will introduce how to alias the fields and tables Create aliases and how to use them.

  1. alias the table

When the table name is very long or some special queries are executed, for the convenience of operation or when the same table needs to be used multiple times, you can specify an alias for the table, and use this alias to replace the original name of the table. The basic syntax for aliasing a table is: 

表名 [AS] 表别名

 "Table name" is the name of the data table stored in the database, "table alias" is the new name of the table specified when querying, and the AS keyword is an optional parameter.

Take the alias o for the orders table and query the order date of the 30001 order. The SQL statement is as follows: 

SELECT * FROM orders AS o WHERE o.o_num = 30001;

 Here, the code of orders AS o indicates that the order table is aliased as o. When specifying the filter condition, o is directly used instead of orders. The query results are as follows:

 

Take aliases for the customers table and the orders table respectively, and perform a connection query. The SQL statement is as follows: 

 SELECT c.c_id, o.o_num FROM customers AS c LEFT OUTER JOIN orders AS o ON c.c_id = o.c_id;

 It can be seen from the results that MySQL can take aliases for multiple tables at the same time, and table aliases can be placed in different positions, such as WHERE clause, SELECT list, ON clause, and ORDER BY clause. When introducing the inner join query earlier, it was pointed out that self-join is a special kind of inner join. The two tables in the join query are the same table. The query statement is as follows:

 SELECT f1.f_id, f1.f_name FROM fruits AS f1, fruits AS f2 WHERE f1.s_id = f2.s_id AND f2.f_id = 'a1';

 

Here, if you don't use table aliases, MySQL will not know which fruits table instance is being referenced, which is a very useful place for table aliases.

Tip: When aliasing a table, make sure it does not conflict with the names of other tables in the database. 

2. Alias ​​the fields 

As can be seen from the examples in this chapter and previous chapters, when using the SELECT statement to display query results, MySQL will display the output columns specified after each SELECT. In some cases, the names of the displayed columns will be very long or not enough. Intuitively, MySQL can specify column aliases, replacement fields or expressions. The basic syntax for aliasing a field is: 

列名 [AS] 列别名

 "Column name" is the name defined by the field in the table, "Column alias" is the new name of the field, and the AS keyword is an optional parameter.

Query the fruits table, take the alias fruit_name for f_name, take the alias fruit_price for f_price, take the alias f1 for the fruits table, and query the names of the fruits whose f_price < 8 in the table, the SQL statement is as follows:

 SELECT f1.f_name AS fruit_name, f1.f_price AS fruit_price FROM fruits AS f1 WHERE f1.f_price < 8;

 You can also alias computed fields in the SELECT clause. For example, aliasing result fields performed with aggregate functions such as COUNT or system functions such as CONCAT.

Query the fields s_name and s_city in the suppliers table, use the CONCAT function to connect the two field values, and take the column alias as suppliers_title.

If there is no alias for the connected value, the displayed column name will not be intuitive enough. The SQL statement is as follows:

 SELECT CONCAT(TRIM(s_name) , ' (', TRIM(s_city), ')') FROM suppliers ORDER BY s_name;

 

 

 It can be seen from the results that the column name of the displayed result is the calculated field after the SELECT clause. In fact, the column after calculation has no name. This result is not easy to understand. If you take an alias for the field, it will be To make the result clear, the SQL statement is as follows:

 SELECT CONCAT(TRIM(s_name) , ' (', TRIM(s_city), ')') AS suppliers_title FROM suppliers
 ORDER BY s_name;

 

It can be seen from the results that AS suppliers_title is added after the calculated field value in the SELECT clause, which instructs MySQL to create an alias suppliers_title for the calculated field, and the displayed result is the specified column alias, which enhances the readability of the query results.

Tip: The table alias is only used when executing the query, and will not be displayed in the returned result. After the column alias is defined, it will be returned to the client for display, and the displayed result field is the alias of the field column

Guess you like

Origin blog.csdn.net/weixin_45392969/article/details/130051777