第七课 创建计算的字段(Creating Calculated Fields)
本课学习什么是计算的字段,如何创建计算字段,以及如何在你的应用中使用其别名。
7.1 理解计算的字段(Understanding Calculated Fields)
存储在数据库表中的数据经常不是你的应用需要的格式。有时你需要的是从数据库表中经过转换的,计算过的以及重新格式化过的数据。
Field
本质上与column等价,经常可交换使用,尽管数据库列典型称为columns,术语fields经常连同计算字段一起使用。
Tips:客户端与服务端格式相反
许多转换及重新格式化可在SQL声明中进行,也可直接在客户端应用中进行。然而,通常来说,在数据库服务端做这些操作比在客户端更快。
7.2 连接字段(Concatenating Fields)
Concatenate(连接)
将值连接在一起(彼此附加)来形成单个长值。
Note:采用+ 或者 || 来连接,取决于你使用的数据库。
输入:
SELECT vend_name + ' (' + vend_country + ')'
FROM Vendors
ORDER BY vend_name;
等价于:
输入:
SELECT vend_name || ' (' || vend_country || ')'
FROM Vendors
ORDER BY vend_name;
输入:
SELECT Concat(vend_name, ' (', vend_country, ')')
FROM Vendors
ORDER BY vend_name;
上面的SELECTC声明包含下列元素:
1、保存在vend_name列中的名字。
2、包含空格和(的字符串。
3、保存在vend_country列中的状态。
4、包含)的字符串。
输入:
SELECT RTRIM(vend_name) + ' (' + RTRIM(vend_country) +')'
FROM Vendors
ORDER BY vend_name;
Note:The TRIM Functions
RTRIM将vend_country右侧的空格去掉。
LTRIM将vend_country左侧的空格去掉。
TRIM将vend_country两边的空格去掉。
使用别名(Using Aliases)
AS关键字
输入
SELECT RTRIM(vend_name) + ' (' + RTRIM(vend_country) + ')'
AS vend_title
FROM Vendors
ORDER BY vend_name;
输入
SELECT RTRIM(vend_name) || ' (' || RTRIM(vend_country) || ')'
AS vend_title
FROM Vendors
ORDER BY vend_name;
输入
SELECT Concat(vend_name, ' (', vend_country, ')')
AS vend_title
FROM Vendors
ORDER BY vend_name;
Note: AS Often Optional
Use of the AS keyword is optional in many DBMSs, but using it is
considered a best practice.
Tip: Other Uses for Aliases
Aliases have other uses too. Some common uses include renaming a
column if the real table column name contains illegal characters (for
example, spaces), and expanding column names if the original names
are either ambiguous or easily misread.
Caution:Alias Names
Aliases may be single words or complete strings. If the latter is used
then the string should be enclosed within quotes. This practice is legal,
but is strongly discouraged. While multiword names are indeed highly
readable, they create all sorts of problems for many client applications.
So much so, that one of the most common uses of aliases is to rename
multiword column names to single word names (as explained above).
Note: Derived Columns
Aliases are also sometimes referred to as “derived columns,” so
regardless of the term you run across, they mean the same thing.
7.2 做数学计算
输入
SELECT prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
FROM OrderItems
WHERE order_num = 20008;
Table 7.1. SQL Mathematical Operators

Tip: How to Test Calculations
SELECT provides a great way to test and experiment with functions
and calculations. Although SELECT is usually used to retrieve data
from a table, the FROM clause may be omitted to simply access and
work with expressions. For example, SELECT 3 * 2; would return
6, SELECT Trim(' abc '); would return abc, and SELECT
Now(); uses the Now() function to return the current date and time.
You get the idea—use SELECT to experiment as needed.
7.3 总结
In this lesson, you learned what calculated fields are and how to create them. You used examples demonstrating the use of calculated fields for both string concatenation and mathematical operations. In addition, you learned how to create and use aliases so that your application can refer to calculated fields.