Database SQL Statements Learning notes (5)-Creating calculated fields

Source: Internet
Author: User
Tags arithmetic select from where

1. The data stored in the table is not always required by the application, for example, we have the unit price of the goods, there are two columns of the number of items sold, but we need the total price information, that is, the unit price * product for printing the invoice. Therefore, we need to retrieve the converted, computed or formatted data directly from the database, rather than retrieving the data. At this point the unit price and quantity are normal table columns, and the total price is the calculated field.

    • Field: Basically the same as the column, often used with each other, but database columns are generally called columns.
    • Only the database knows which columns are columns and which columns are fields. The application is not recognized.
 Use sells; SELECT *  from Vendors;

2. Stitching (CONCATENATE) field: Joins values together (attaches one value to another value) to form a single value.

 Use sells; SELECT concat (vend_name,' (', Vend_country,')'    from vendors;

Sometimes in order to correctly return formatted data, some spaces must be removed, which can be done with the RTrim () function.

LTRIM (remove all the blanks on the left), TRIM () strips the left and right sides of the string.

3. When stitching is complete, a new column is created, but there is no name so that it cannot be referenced when it is used. Thus, we need to give it an alias, given by using the AS keyword.

SELECT concat (Trim (vend_name),'  (', Trim (vend_country),'  )' ) as from vendors;  

Results:

4. Perform arithmetic operations

 SELECT from WHERE order_num = 20008;

Now calculate the total price and alias Overall_price

SELECT  prod_id,quantity,item_price,quantity  * as from WHERE order_num=20008;

Overall_price is the alias of the new field, also known as the Export column.

SQL arithmetic operator: +,-,*,/

SELECT 2 * 3;

The result above is 6, which means that select can be used not only as a keyword to retrieve data, but also as a simple way to access and manipulate an expression

SELECT now ();

Show Current Time 2016-06-06 20:22:46

Database SQL Statements Learning notes (5)-Creating calculated fields

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.