[MySQL Must Know and Know (7)] [Create Calculated Field]

Previous: [MySQL must know and know (6)] [Search with regular expressions]

+++++++++++++Start line++++++++++++++++

One, calculated field

The calculated field does not actually exist in the database table, but is created in the SELECT statement at runtime

Field

Basically, it has the same meaning as a column and is often used interchangeably. However, a database column is generally called a column, and the term field is usually connected to a calculated field.

Client and server format

Many of the conversion and formatting tasks that can be done within the SQL statement can be done directly in the client application. But generally speaking, these operations are completed much faster on the database server than on the client, because the DBMS is designed to complete this processing quickly and efficiently.

Two, splicing fields

Splicing

Join values ​​together to form a single value

The difference of MySQL
Most DBMS use + or || to achieve splicing, while MySQL uses the Concat() function to achieve

mysql> SELECT Concat(vend_name, '(', vend_country,')')
    -> FROM vendors
    -> ORDER BY vend_name;

Insert picture description here

Delete the extra spaces on the right side of the data to organize the data, you can use the RTrim() function of MySQL to complete

mysql> SELECT Concat(RTrim(vend_name), '(', RTrim(vend_country),')')
    -> FROM vendors
    -> ORDER BY vend_name;

Insert picture description here

2.1 Using aliases

An alias is an alternative name for a field or value, and the alias is assigned using the AS keyword.

mysql> SELECT Concat(RTrim(vend_name),'(',RTrim(vend_country),')') AS vend_title
    -> FROM vendors
    -> ORDER BY vend_name;

Insert picture description here

Three, perform arithmetic calculations

mysql> SELECT prod_id, quantity, item_price
    -> FROM orderitems
    -> WHERE order_num = 20005;

Insert picture description here

mysql> SELECT prod_id, quantity, item_price, quantity*item_price AS expanded_price
    -> FROM orderitems
    -> WHERE order_num = 20005;

Insert picture description here

+++++++++++++End line++++++++++++++++

Next: [MySQL Must Know and Know (8)] [Use Data Processing Function]

Guess you like

Origin blog.csdn.net/qq_42893334/article/details/108772419