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

上传人:206****923 文档编号:41638995 上传时间:2018-05-30 格式:DOC 页数:5 大小:57KB
返回 下载 相关 举报
SQL Server表与列别名,计算列等等的说明和例子_第1页
第1页 / 共5页
SQL Server表与列别名,计算列等等的说明和例子_第2页
第2页 / 共5页
SQL Server表与列别名,计算列等等的说明和例子_第3页
第3页 / 共5页
SQL Server表与列别名,计算列等等的说明和例子_第4页
第4页 / 共5页
SQL Server表与列别名,计算列等等的说明和例子_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《SQL Server表与列别名,计算列等等的说明和例子》由会员分享,可在线阅读,更多相关《SQL Server表与列别名,计算列等等的说明和例子(5页珍藏版)》请在金锄头文库上搜索。

1、语句所用表说明第一章:1.select 语句中使用列别名在 select 语句中使用列别名的例子中利用的表是学生表(studenttable),学生表中有字 段 studentid(学生学号) 从 1 开始自动增长 1,主键,不可以为空。 studentname(学生姓名)不可以为空。 studentsex(学生性别)唯一约束,只能为男或者女,不可以为空。 studentage(学生年龄)唯一约束,年龄范围在 10-35 之间,不可以为空。在 select 语句中使用列别名有四种使用方法: (1)用 as 关键字 例:select studentid as 学号 from studenttal

2、e 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),商品表中有字

3、段 Commodityid(商品编号)从 1 开始自动增长 1,主键,不可以为空。 Commodityname(商品名称)不可为空。 Commoditystock(商品进货数量)不可为空。 Commoditysell(商品售出数量)不可为空。 Commodityunit(商品进价)不可为空。 Commoditybid(商品单价)不可为空。 (1)计算库存数量 例:select (commoditystock-commoditysell)as 库存数量 from commoditytable (2)计算已卖商品利润 例:select (commoditybid-commodityunit)*co

4、mmoditysell 已卖商品获得利润 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 stude

5、nttable “student” (3)不带引号例:select student.* from studenttable student 三种方法中均可以给 where 条件 例:select student.* from studenttable student where student.Studentid = 值 注意:列别名可以用单引号( ) ,而表别名不可以用单引号。 4在 where 子句使用计算列Where 子句中使用计算列和在 select 语句中使用计算列几乎相同,所以在此例中我们利用商品 表(commoditytable)进行说明。 (1)计算库存数量为 5 的商品信息

6、例:select commodity.* from commoditytable as commodity where (moditystock-moditysell) = 5(2)计算已卖商品利润大于 35 的商品信息 例:select commodityid as 编号,commodityname 商品名称,commoditystock 进货总量,commoditysell “已售数量“,commodityunit 进价,commoditybid 单价 from commoditytable where (commoditybid- commodityunit) 35(3)计算库存利润小于

7、 200 的商品信息 例:select commodity.* from commoditytable as commodity Where (moditystock moditysell)*(moditybid- commodity.unit) 200 注意:select 语句中使用计算列是可以直接给出计算结果的,而在 where 子句中只能做对比的, 5利用 beginend 批处理执行多条 sql 语句 Beginend 是可以一次性处理多条 sql 语句的批处理,我们这里利用学生表 (studenttable)举例说明 (1)输出年龄小于 23 的学生的姓名(studentname)

8、,性别(studentsex) 例:declare studentid int,studentname varchar(15),studentsex char(5) ,studentage intSelect studentname=student.studentname,studentsex=student.studentsex ,studentage=student.studentage from studenttable student Where studentage 23If studentage = 21 Begin Print 姓名:+studentname Print性别:+s

9、tudentsex End Else Print 不存在的记录在 if 语句中如果没有 beginend 语句的话执行的时候会报关键字 else 附近有语法错误, 还有就是在本例中为什么我没有输出学号和年龄,因为这两字段都为 int 类型,如果我按 print 学号: +studentid 这样输出的话他会说“在将 varchar 值学号: 转换成数据类型 int 时失败”如 果直接 print studentid 的话则可以 6利用 top 关键字查询指定行数据 利用 top 关键字可以查询指定的行数据,这里我们利用学生表(studenttable)具体说 明 语法:select top n

10、 percent select_list from tablenamen:为整数 percent:为可选参数,代表返回所有记录的百分之 n (1)查询学生表(studenttable)中的前两条记录 例:select top 2 * from studenttable (2)查询学生表(studenttable)中的%5 的数据 例:select top 5 percent * from studenttable 7去除结果集中的重复行利用 distinct 关键字可以去除结果集中的重复行,这里我们利用订单表(ordertable)举 例说明。 语法:select distinct|all s

11、elect_list from tablename All 是语句的默认行为,如果不写 distinct 和 all 的话默认为 all。 例: (1)查询表中所有记录去掉 order_id 重复的记录 Select distinct order_id,user_name,num_tot,ord_status,ord_date from ordertable (2)查询表中记录用 order_id 去除重复记录的总数 Select count(*) from (select distinct order_id from ordertable) as ot 8.计算数据在结果集中的行号 如果一个

12、表中没有主键也没有自动编号,那么要统计数据的行号就是很难的, 这里我 们使用订单表(ordertable)表举例说明利用 ROW_NUMBER()函数可以获得结果集中的行号。 语法:ROW_NUMBER() OVER(排序字段) 例: (1)查询出 ordertable 表中的所有字段并且按 ord_date 升序排列 Select row_number over(order by ord_date asc) as 行号, ord_id,user_name,num_tot,ord_status,ord_date from ordertable 注意:使用row_number()函数必须要有or

13、der by子句,否则没有办法计算行号 9随机查询n行记录 这里我们利用学生表(studenttable)举例说明,利用newid()函数可以随机查 询n行记录和随机排序 (1)随机排序 例:select * from studenttable order by newid() (2)随机查询 n 行记录 例:select top n from studenttable order by newid() 注意:neweid()函数必须和 order by 子句一起使用,如果没有 order by 子句则执行错误 10格式化结果集数据 本节我们利用 convert()函数,cast()函数 su

14、bstring()函数实现格式化结果集数据 A. convert()转化函数 语法:convert(data_typelength,expression style) (1) 把字符串转换为日期 Declare tem varchar(10) Set tem = 20100806 Select convert(datetime,tem) (2) 把日期转化为字符串 Declare tem datetimeSet tem =2010-08-06 Select convert(varchar,tem) 在表中,左侧的两列表示将 datetime 或 smalldatetime 转换为字符数据的 s

15、tyle 值。给 style 值加 100,可获得包括世纪数位的四位年份 (yyyy)。 不带世纪数位 (yy) 带世纪数位 (yyyy) 标准 输入/输出* - 0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM) 1 101 美国 mm/dd/yyyy 2 102 ANSI yy.mm.dd 3 103 英国/法国 dd/mm/yy 4 104 德国 dd.mm.yy 5 105 意大利 dd-mm-yy 6 106 - dd mon yy 7 107 - mon dd, yy 8 108 - hh:mm:ss - 9 或 109 (*) 默认值 + 毫秒 m

16、on dd yyyy hh:mi:ss:mmmAM(或 PM) 10 110 美国 mm-dd-yy 11 111 日本 yy/mm/dd 12 112 ISO yymmdd - 13 或 113 (*) 欧洲默认值 + 毫秒 dd mon yyyy hh:mm:ss:mmm(24h) 14 114 - hh:mi:ss:mmm(24h) - 20 或 120 (*) ODBC 规范 yyyy-mm-dd hh:mm:ss.fff - 21 或 121 (*) ODBC 规范(带毫秒) yyyy-mm-dd hh:mm:ss.fff - 126(*) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格) - 1

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号