别名(alias)是一个字段或值的替换名。别名用AS关键字赋予。请看下面的SELECT语句:
输入 SELECT RTRIM(vend_name) + ' (' + RTRIM(vend_country) + ')' AS vend_title FROM Vendors ORDER BY vend_name; 输出 vend_title ----------------------------------------------------------- Bear Emporium (USA) Bears R Us (USA) Doll House Inc. (USA) Fun and Games (England) Furball Inc. (USA) Jouets et ours (France)
下面是MySQL和MariaDB中使用的语句:
输入 SELECT Concat(vend_name, ' (', vend_country, ')') AS vend_title FROM Vendors ORDER BY vend_name;
7.3 执行算术计算
Orders表包含收到的所有订单,OrderItems表包含每个订单中的各项物品。下面的SQL语句检索订单号20008中的所有物品:
输入 SELECT prod_id, quantity, item_price FROM OrderItems WHERE order_num = 20008; 输出 prod_id quantity item_price ---------- ----------- --------------------- RGAN01 5 4.9900 BR03 5 11.9900 BNBG01 10 3.4900 BNBG02 10 3.4900 BNBG03 10 3.4900
item_price列包含订单中每项物品的单价。如下汇总物品的价格(单价乘以订购数量):
输入 SELECT prod_id, quantity, item_price, quantity*item_price AS expanded_price FROM OrderItems WHERE order_num = 20008; 输出 prod_id qua