存儲在數據庫表中的數據一般不是應用程序所需要的格式。所以這個時候就需要使用計算字段來輸出自己需要的格式。
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這樣的一個計算字段,就想其他的列一樣。
基本的算術操作符如下:
加:+ 減:- 乘:* 除:/