存储在数据库表中的数据一般不是应用程序所需要的格式。所以这个时候就需要使用计算字段来输出自己需要的格式。
1.拼接字段
根据DBMS的不同,拼接所表示的语法有所不同。
Oracle和SQLite中使用||操作符。
select vend_name || '(' || vend_country || ')'
from Vendors
order by vend_name;
MySQL使用Contact函数
select Concat( vend_name,vend_country )
from Vendors
order by vend_name;
这个新计算得到的这个字段在DB是不存在的,所以也就没有名字。为了能引用需要一个别名,这就要as关键字。
select concat(vent_name, vent_country)
as vent_title
from Vendors
order by vend_name;
2.字段的计算
select prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
from OrderItems
where order_num = 20008;
会输出一个expanded_price这样的一个计算字段,就想其他的列一样。
基本的算术操作符如下:
加:+
减:-
乘:*
除:/