SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL Aliases - SQL基础教程 - 笔下光年
网站首页
SQL Aliases
In this tutorial you will learn how to specify a short alias name for a table or a table column within an SQL statement. ## Defining Table Aliases When multiple tables are being joined in a single query, you need to prefix each column name with the name of the table it belongs to, like employees.dept_id, departments.dept_id, etc. in order to avoid the confusion and ambiguous column error in case columns in different tables have the same name. But, if table names are long and appears several times in the query then writing the query would become a difficult and annoying task. So to save time and avoid writing the complete table names, you can give each table a short alias name and refer to its columns using that alias name in the query. To understand this clearly, let's look at the following employees and departments tables. ```sql +--------+--------------+------------+---------+ | emp_id | emp_name | hire_date | dept_id | +--------+--------------+------------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 4 | | 2 | Tony Montana | 2002-07-15 | 1 | | 3 | Sarah Connor | 2005-10-18 | 5 | | 4 | Rick Deckard | 2007-01-03 | 3 | | 5 | Martin Blank | 2008-06-24 | NULL | +--------+--------------+------------+---------+ ``` Table: employees ```sql +---------+------------------+ | dept_id | dept_name | +---------+------------------+ | 1 | Administration | | 2 | Customer Service | | 3 | Finance | | 4 | Human Resources | | 5 | Sales | +---------+------------------+ ``` Table: departments Here's a query that retrieves the employee's id, name and their department name by joining the employees and departments tables together using the common dept_id field. ```sql SELECT employees.emp_id, employees.emp_name, departments.dept_name FROM employees LEFT JOIN departments ON employees.dept_id = departments.dept_id ORDER BY emp_id; ``` Here's the compact version of the previous query that uses table aliases: ```sql SELECT t1.emp_id, t1.emp_name, t2.dept_name FROM employees AS t1 LEFT JOIN departments AS t2 ON t1.dept_id = t2.dept_id ORDER BY emp_id; ``` If you execute any of these statements, you'll get the same output, as follow: ```sql +--------+-----------------+--------------------+ | emp_id | emp_name | dept_name | +--------+-----------------+--------------------+ | 1 | Ethan Hunt | Human Resources | | 2 | Tony Montana | Administration | | 3 | Sarah Connor | Sales | | 4 | Rick Deckard | Finance | | 5 | Martin Blank | NULL | +--------+-----------------+--------------------+ ``` As you can see how much typing effort we can save by using the table aliases. Check out the SQL JOINS section to learn more about table joins. ## Defining Aliases for Table Columns In MySQL, when you use the SQL function to generate a customized output the name of the output column might not be human readable or very difficult to understand. In that case, you can use the aliases to temporarily give a different name to the output column. Consider the following query in which we've used an expression to reformat the dates in the hire_date column for generating a custom output: ```sql -- Syntax for MySQL Database SELECT emp_name, DATE_FORMAT(hire_date, '%M %e, %Y') FROM employees; ``` If you execute the above statement, you'll get the output something like this: ```sql +--------------+-------------------------------------+ | emp_name | DATE_FORMAT(hire_date, '%M %e, %Y') | +--------------+-------------------------------------+ | Ethan Hunt | May 1, 2001 | | Tony Montana | July 15, 2002 | | Sarah Connor | October 18, 2005 | | Rick Deckard | January 3, 2007 | | Martin Blank | June 24, 2008 | +--------------+-------------------------------------+ ``` As you see the label of the last column in our output is long and unwieldy. We can fix this problem using the column aliases, as follow: ```sql -- Syntax for MySQL Database SELECT emp_name, DATE_FORMAT(hire_date, '%M %e, %Y') AS hire_date FROM employees; ``` If you execute the above statement, you'll get more readable output, as follow: ```sql +--------------+------------------+ | emp_name | hire_date | +--------------+------------------+ | Ethan Hunt | May 1, 2001 | | Tony Montana | July 15, 2002 | | Sarah Connor | October 18, 2005 | | Rick Deckard | January 3, 2007 | | Martin Blank | June 24, 2008 | +--------------+------------------+ ``` <div class="callout callout-info mb-3">Note: You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column. However, aliases in a WHERE clause is not allowed.《、》
上一篇:
SQL Alter Table
下一篇:
SQL Group By