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

上传人:野鹰 文档编号:3175027 上传时间:2017-07-31 格式: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 studenttale where

2、 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(商

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

4、om commdoitytable(3)计算库存利润例:select (commoditystock-commoditysell)* (commoditybid-commodityunit ) as 库存利润From commdoitytable3.使用表别名查询数据表别名和列别名几乎相同,在此例中我们利用学生表(studenttable)进行说明(1)利用 as 关键字例:select student.* from studenttable as student(2)利用双引号例:select student.*from studenttable “student”(3)不带引号例:sele

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

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

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

8、e varchar(15),studentsex char(5) ,studentage intSelect studentname=student.studentname,studentsex=student.studentsex ,studentage=student.studentage from studenttable student Where studentage 23If studentage = 21BeginPrint 姓名:+studentnamePrint性别:+studentsexEndElsePrint 不存在的记录在 if 语句中如果没有 beginend 语句的

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

10、录的百分之 n(1)查询学生表(studenttable)中的前两条记录例:select top 2 * from studenttable(2)查询学生表(studenttable)中的%5 的数据例:select top 5 percent * from studenttable7去除结果集中的重复行利用 distinct 关键字可以去除结果集中的重复行,这里我们利用订单表(ordertable)举例说明。语法:select distinct|all select_list from tablenameAll 是语句的默认行为,如果不写 distinct 和 all 的话默认为 all。例

11、:(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 ot8.计算数据在结果集中的行号如果一个表中没有主键也没有自动编号,那么要统计数据的行号就是很难的, 这里我们使用订单表(ordertable)表举例说明利用 ROW_NUMBER()函数可以获

12、得结果集中的行号。语法: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()函数必须要有order by子句,否则没有办法计算行号9随机查询n行记录这里我们利用学生表(studenttable )举例说明,利用newid()函数可以随机查询n行记录和随机排

13、序(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()函数 substring()函数实现格式化结果集数据A. convert()转化函数语法:convert (data_typelength,expression style)(1) 把字符串

14、转换为日期Declare tem varchar(10)Set tem = 20100806Select convert(datetime,tem)(2) 把日期转化为字符串Declare tem datetimeSet tem =2010-08-06Select convert(varchar,tem)在表中,左侧的两列表示将 datetime 或 smalldatetime 转换为字符数据的 style 值。给 style 值加 100,可获得包括世纪数位的四位年份 (yyyy)。 不带世纪数位 (yy) 带世纪数位 (yyyy) 标准 输入/输出* - 0 或 100 (*) 默认值 m

15、on 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 (*) 默认值 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM) 10 110 美国 mm-dd-yy 11 111 日本 yy/mm/dd 12 112 ISO yymmdd - 13 或 113

16、(*) 欧洲默认值 + 毫秒 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(不含空格) - 130* 科威特 dd mon yyyy hh:mi:ss:mmmAM - 131* 科威特 dd/mm/yy hh:mi:ss:mmmAMB. cast()转换函数语法:cast(expre

展开阅读全文
相关资源
相关搜索

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

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