SQL Getting Sta...login
SQL Getting Started Tutorial Manual
author:php.cn  update time:2022-04-12 14:15:40

SQL alias



By using SQL, you can specify aliases for table names or column names.


SQL Aliases

Using SQL, you can specify aliases for table names or column names.

Basically, aliases are created to make column names more readable.

SQL alias syntax for columns

SELECT column_name AS alias_name
FROM table_name;

SQL alias syntax for table

SELECT column_name(s)
FROM table_name AS alias_name;


Demo Database

In this tutorial, we will use the php sample database.

The following is the data selected from the "Websites" table:

+----+--------------+--- ------------------------+------+---------+
| id | name | url                                                                    --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | php Chinese website | //m.sbmmt.com/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+----------- ----------------+-------+---------+

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 | 201 6-05 -16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+----------+------+--- ---------+
9 rows in set (0.00 sec)

Alias ​​instance of column

The following SQL statement specifies two Alias, one is the alias of the name column, and the other is the alias of the country column. Tip: If the column name contains spaces, double quotes or square brackets are required:

Example

SELECT name AS n, country AS c
FROM Websites ;

Execution output result:

In the following SQL statement, we put four columns (name, url, alexa and country) Combined together and create an alias called "site_info":

Instance

                SELECT name, CONCAT(url, ', ', alexa, ', ', country) AS site_info
FROM Websites;

Execution output result:



Table alias example

The following SQL statement selects the accessed records of "php Chinese website". We use the "Websites" and "access_log" tables and give them table aliases "w" and "a" respectively (using aliases makes the SQL shorter):

Example

SELECT w.name, w.url, a.count, a.date
FROM Websites AS w, access_log AS a
WHERE a.site_id=w.id and w.name="php中文网";

Execution output result:

The same SQL statement without alias:

Example

SELECT Websites.name, Websites.url, access_log.count, access_log.date
FROM Websites, access_log
WHERE Websites.id=access_log.site_id and Websites.name="php中文网";

Execution output result:

In the following situations, using aliases is useful:

  • In More than one table is involved in the query

  • A function is used in the query

  • The column name is very long or poorly readable

  • Need to combine two or more columns