码迷,mamicode.com
首页 > 数据库 > 详细

2014.9.9数据库高级查询

时间:2014-09-09 22:58:09      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   数据   2014   art   

1 select * from Nation
2 select * from Info

bubuko.com,布布扣

1 --连接(列的扩展)
2 --把两个表连起来显示(第一种方法)
3 select Info.Code,Info.Name,Info.Sex,Nation.Name as nation,Info.Birthday 
4 from Info,Nation 
5 where Nation.Code=Info.Nation --笛卡尔积  --把列名换成汉字 [as]空格+列名

执行结果:

bubuko.com,布布扣

1 --把两个表连接起来(第二种方法)
2 select * from Info join Nation on Info.Nation=Nation.Code where Nation.Name=汉族--后面可以加where条件
1 select * from Info left join Nation on Info.Nation=Nation.Code--以左边为主,NULL值也会显示出来,左连接
2 select * from Info right join Nation on Info.Nation=Nation.Code--以右边为主,NULL值也会显示出来,右连接
3 select * from Info full join Nation on Info.Nation=Nation.Code--外连接(左,右,全连接)

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

 1 --联合(行的扩展)
 2 select Name from Info
 3 union
 4 select Name from Family
 5 --子查询(嵌套查询)
 6  select Name from Info where Code in (select infocode from Work group by InfoCode having COUNT(*)=3)
 7  
 8  --取前10条信息
 9  select top 10 * from Car 
10  --分页查询,查询第二页
11  select top 5 * from Car where Code not in
12  (
13     select top 5 Code from Car
14  )
1 --取总页数
2 select CEILING(COUNT(*)/5.0) from car
1 --ceiling返回进1的值,floor返回退1的值,round四舍五入
2 print ceiling(6.14)  --结果为7
3 print floor(5.73)   --结果为5
4 print round(345.863,1)  --结果为345.900  保留一位小数
5 print round(345.863,-2)  --结果为300.00  小数点往左去两位
6 print round(345.863,0)   --结果为346,000  保留0位小数
1 print sqrt(4)--返回表达式的平方根
2 
3 print rand()--0-1之间的随机

ABS()  绝对值

LOWER()  把字符串全部转换为小写

UPPER()  把字符串全部转换为大写

STR()  把数值型数据转换为字符型数据

select 平均分是:+STR(AVG(degree)) from score

LTRIM()  把字符串头部的空格去掉

RTRIM()  把字符串尾部的空格去掉

LEFT()  返回部分字符串

RIGHT()  返回部分字符串

SUBSTRING()  返回部分字符串  (哪个位置,长度)

REVERSE()  将指定的字符串的字符排列顺序颠倒

REPLACE()  返回被替换了指定子串的字符串

select replace(abc123g,123,def)  --返回结果为abcdefg

数据类型转换函数:

select cast(12345 as char) --12345
select convert(int,3.14) --返回结果为3
select convert(bit,12.345) --返回结果为1,bit逻辑类型

DAY() 返回date_expression 中的日期值 DAY(<date_expression>)

MONTH()返回date_expression 中的月份值 MONTH(<date_expression>)

YEAR()返回date_expression 中的年份值 YEAR(<date_expression>)

DATEADD 函数返回指定日期date 加上指定的额外日期间隔number 产生的新日期

DATEADD(<datepart>,<number>,<date>)

DATEDIFF()  返回date2 超过date1的差距值,其结果值是一个带有正负号的整数值 DATEDIFF(<datepart>,<date1>,<date2>)

DATEPART()   以整数值的形式返回日期的指定部分 DATEPART(<datepart>,<date>)

DATEPART(dd,date)等同于DAY(date)

DATEPART(mm,date)等同于MONTH(date)

DATEPART(yy,date)等同于YEAR(date)

getdate()  获取当前日期

ISDATE ()判断所给定的表达式是否为合理日期,如果是则返回1,不是则返回0. ISDATE(<expression>)

2014.9.9数据库高级查询

标签:style   blog   http   color   io   ar   数据   2014   art   

原文地址:http://www.cnblogs.com/zsmj001/p/3963476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!