MySQL SQL alias usage and alias (alias) syntax

Source: Internet
Author: User
Tags aliases joins


SELECT column_name (s) from table_name as Alias_name


SELECT column_name as Alias_name from table_name


Suppose we have two tables: "Persons" and "Product_orders" respectively. We assign them the alias "P" and "Po" respectively. Now, we want to list all orders for "John Adams". We can use the following SELECT statement: Select PO. OrderID, P.lastname, p.firstname from Persons as P, product_orders as Po where p.lastname= ' Adams ' where p.firstname= ' John '

Select statements that do not use aliases: Select Product_orders.orderid, Persons.lastname, persons.firstname from Persons, Product_orders WHERE P Ersons. Lastname= ' Hansen ' WHERE persons.firstname= ' Ola '

From the top two SELECT statements, you can see that aliases make it easier for the query to read and write.





Id
LastName
FirstName
Address
City


1
Adams
John
Oxford Street
London


2
Bush
George
Fifth Avenue
New York


3
Carter
Thomas
Changan Street
Beijing




SELECT LastName as Family, FirstName as Name from Persons






Family
Name


Adams
John


Bush
George


Carter
Thomas




Next, we discuss the use of alias (alias) on SQL. There are two types of aliases that are most commonly used: field aliases and table aliases.

Simply put, the purpose of a field alias is to make the results of SQL production easier to read. In the previous example, when we had the sum of turnover, the field name was sum (sales). Although there is no problem in this case, if the field is not a simple sum, but a complex calculation, the field name is not so easy to understand. If we use a field alias, we can confirm that the field name in the result is simple and understandable.

The second type of alias is a table alias. To give a table an alias, just empty the table name in the FROM clause and then list the table aliases you want to use. This is handy when we're going to use SQL to get information from several different tables. That's what we'll see when we talk about joins (joins) later.

Let's take a look at the syntax of the field alias and the table alias:

Select "Table alias". " Field 1 "" Field alias from "Table name" "Table alias"

Basically, both aliases are placed behind the objects they want to replace, and the middle is separated by a blank space. We continue to use the Store_information form as an example:

Store_information table

Store_namesalesdate
Los angeles$1500jan-05-1999
San francisco$300jan-08-1999
boston$700jan-08-1999

We use the same example as the SQL GROUP by page. The difference here is that we added a field alias and a form alias:

SELECT A1.store_name Store, SUM (A1. Sales) ' Total Sales ' from Store_information A1 GROUP by A1.store_name

Results:

Store Total Sales
Los Angeles $1800
San Diego $
Boston $700
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.