SQL Server表与列别名,计算列等等的说明和例子.docVIP

  • 1
  • 0
  • 约5.09千字
  • 约 5页
  • 2016-09-10 发布于天津
  • 举报

SQL Server表与列别名,计算列等等的说明和例子.doc

SQL Server表与列别名,计算列等等的说明和例子.doc

语句所用表说明 第一章: 1.select 语句中使用列别名 在select 语句中使用列别名的例子中利用的表是学生表(studenttable),学生表中有字段 studentid(学生学号) 从1开始自动增长1,主键,不可以为空。 studentname(学生姓名)不可以为空。 studentsex(学生性别)唯一约束,只能为男或者女,不可以为空。 studentage(学生年龄)唯一约束,年龄范围在10-35之间,不可以为空。 在select语句中使用列别名有四种使用方法: (1)用as关键字 例:select studentid as 学号 from studenttale where studentname = ‘值’ (2)用双引号 例:select studentid “学号” from studenttable where studentname = ‘值’ (3)用单引号 例:select studentid ‘学号’ from studenttable where studentname =’值’ (4)不带引号 例:select studentid 学号 from studenttable where studentname =’值’ 2.select 语句中使用计算列 在select语句中使用计算列利用的表是商品表(commoditytable),商品表中有字段 Commodityid(商品编号)从1开始自动增长1,主键,不可以为空。 Commodityname(商品名称)不可为空。 Commoditystock(商品进货数量)不可为空。 Commoditysell(商品售出数量)不可为空。 Commodityunit(商品进价)不可为空。 Commoditybid(商品单价)不可为空。 (1)计算库存数量 例:select (commoditystock-commoditysell)as 库存数量 from commoditytable (2)计算已卖商品利润 例:select (commoditybid-commodityunit)*commoditysell ‘已卖商品获得利润’ from commdoitytable (3)计算库存利润 例:select (commoditystock-commoditysell)* (commoditybid-commodityunit) as 库存利润 From commdoitytable 3.使用表别名查询数据 表别名和列别名几乎相同,在此例中我们利用学生表(studenttable)进行说明 (1)利用as关键字 例:select student.* from studenttable as student (2)利用双引号 例:select student.*from studenttable “student” (3)不带引号 例:select student.* from studenttable student 三种方法中均可以给where条件 例:select student.* from studenttable student where student.Studentid = ‘值’ 注意:列别名可以用单引号(‘’),而表别名不可以用单引号。 4.在where子句使用计算列 Where子句中使用计算列和在select语句中使用计算列几乎相同,所以在此例中我们利用商品表(commoditytable)进行说明。 (1)计算库存数量为5的商品信息 例:select commodity.* from commoditytable as commodity where (moditysell) = 5 (2)计算已卖商品利润大于35的商品信息 例:select commodityid as 编号,commodityname 商品名称,commoditystock 进货总量,commoditysell 已售数量, commodityunit 进价,commoditybid 单价from commoditytable where (commoditybid-commodityunit) 35Where (moditystock –moditysell)*(moditybid-commodity.unit) 200 注意:select语句中使用计算列是可以直接给出计算结果的,而在where子句中只能做对比的, 5.利用begin…end批处理执行多条sql语句 Begin…end是可以一次性处理多条sql语句的批处理,我们这里利用学生表(studenttable)举例说明 (1)输出年龄小于

文档评论(0)

1亿VIP精品文档

相关文档