- 1
- 0
- 约5.09千字
- 约 5页
- 2016-09-10 发布于天津
- 举报
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)输出年龄小于
您可能关注的文档
最近下载
- 2025年中石化招聘笔试参考题库含答案解析.docx
- 【巴塔耶】眼睛的故事.pdf VIP
- 医疗器械销售及物流操作规程全文.docx VIP
- 建立自己的“人情账本”.doc VIP
- 2026年高考第一次模拟考试物理一模突破卷03(陕晋宁青专用)(考试版).docx VIP
- 医疗器械运输与配送管理手册.docx VIP
- 2024年高考化学押题预测卷01【江苏卷】及答案解析.docx VIP
- 2026年高考第一次模拟考试物理一模保分卷03(陕晋宁青专用)(考试版).docx VIP
- 松下KR系列主板图纸.pdf VIP
- 2023《企业进行业财融合的现状及问题分析—以格力电器为例》3500字.docx VIP
原创力文档

文档评论(0)