1.as
- 在使用SQL语句显示结果的时候,往往在屏幕显示的字段名并不具备良好的可读性,此时可以使用as给字段起一个别名。
- 注意:as是可以省略的。
- 说明:这里看起来没什么用,“自连接”的时候必须对表起别名
使用as给字段(列)起别名
# select 字段1 as "别名1", 字段2 as "别名2", ... from 表名 where 条件
select name as "名称", price as "单价", expenditure as "花费", weight as "重量", unit as "单位" from price;
// 省略as的写法
// select 字段1 "别名1", 字段2 "别名2", ... from 表名 where 条件
select name "名称", price "单价", expenditure "花费", weight "重量", unit "单位" from price;
通过as给表起别名
# 我们平时写的查询都省略了表名
select 字段1, 字段2,... from 表名;
# 完整的写法:表名.字段名
select 表名.字段1, 表名.字段2, ... from 表名;
# 起别名的写法: 别名.字段名
select 别名.字段1, 别名.字段2, ... from 表名 as 别名;
# 例子
select p.id, p.name, p.price from price as p; //完整写法
select p.id, p.name, p.price from price p; //简写
练习:
select Id,Title,'美国' as country from Movies;