1 计算字段
计算字段并不实际存在于数据库表中,而是运行时在SELECT语句内创建的
2 拼接字段
SQL Server 和 Access:
SELECT vend_name + ' ( ' + vend_country + ' ) ' FROM vendors ORDER BY vend_name;
DB2、Oracle、SQLite:
SELECT vend_name || ' ( ' || vend_country || ' ) ' FROM vendors ORDER BY vend_name;
MySQL:
SELECT CONCAT(vend_name,'(',vend_country,')') FROM vendors ORDER BY vend_name;
TRIM的使用
SELECT CONCAT(RTRIM(vend_name),'(',TRIM(vend_country),LTRIM(' )')) FROM vendors ORDER BY vend_name;
RTRIM():去掉字符串右边的空格
LTRIM():去掉字符串左边的空格
TRIM():去掉字符串左右两边的空格
使用别名
别名(alias)使用AS关键字赋予
SELECT CONCAT(vend_name,'(',vend_country,')') AS vend_title FROM vendors ORDER BY vend_name;
3 执行算术计算
SELECT prod_id,quantity,item_price,quantity*item_price AS expanded_price FROM orderitems WHERE order_num=20008;
SQL算术操作符
操作符 | 说明 |
---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除 |