Linux SQL statement Concise tutorial---Alias

Source: Internet
Author: User
Tags aliases

Next, we discuss the use of the alias (alias) on SQL.

There are two types of aliases that you use most often: field aliases and table aliases.

In a nutshell. The purpose of the field aliases is to make the results of SQL generation easy to read. In the previous example. Every time we have a total turnover, the column name is SUM (sales). Although there is no problem in this case, it is assumed that this field is not a simple summation. It's a complex calculation. That column name is not so easy to understand. If we use field aliases. You can confirm that the field names in the results are easy to understand.

Another alias is a table alias.

To give a table an alias, simply empty the table name in the FROM clause and then list the table alias you want to use.

This is very handy when we want to use SQL to get data from several different tables. This is what we'll see later when we talk about join.

Let's take a look at the syntax for field aliases and table aliases:

Select "Table alias". " Field 1 "Field alias"
From "Table name" "Table alias";

Basically, both of these aliases are placed behind the object they are replacing. And the middle of them is separated by a blank.

We continue to use the store_information form to do a sample:

store_information Form

Store_name Sales Txn_date
Los Angeles 1500 05-jan-1999
San Diego 250 07-jan-1999
Los Angeles 300 08-jan-1999
Boston 700 08-jan-1999

We use the same example as the SQL GROUP by page.

The difference here is that we've added field aliases and table aliases:

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 250
Boston 700

In the results. The information itself is not different. The difference is the title of the field.

This is the result of applying a field alias. In the second field, our title is "Sum (Sales)". And now we have a very clear "total Sales".

It is very obvious that "total sales" can describe the meaning of this field more precisely than Sum (sales).

The advantages of using a table alias are not shown here, but this is very clear on the next page (SQL Join) .


Linux measurements such as the following:




Reprint please specify: Xiao Liu

Linux SQL statement Concise tutorial---Alias

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.