select子句
select id,name from xxiang
id,name为列名,xxiang为表名
使用文字串
select 'the number of''',id from xxiang 要显示单引号,要在''中间要加两个'
select 'the number of"',id from xxiang 要显示双引号,要在''中间要加两个"
显示结果:
无列名 id
the number' of 1
the number' of 2
改变列标题的三种方法
select 'newid'=id, name as newname from xxiang
注意'newid'=id ,'newid'为新的列标题,name as newname,newname为新的列标题,其中as也可省略。
数据运算
select name ,pp,'pptotal'=totals*pp from xxiang
select '圆周率函数'=PI()
go
数据类型的转换
Cast函数和Convert函数
cast(old类型 as new类型)
convert(new类型,old类型)
当convert来转换时间类型时,convert(new,old,style)
style样式的值有101-114,126这些。
select '美国日期样式'=Convert(char(20),GetDate(),101)
显示结果
美国日期样式
12/20/2007
使用all和distinct关键字
all是显示所有重复的数据行,
distinct是仅显示非重复的数据行。
select distinct id from xxiang
排序
asc 升序 是默认的
desc 降序
select * from xxiang order by id desc
top 关键字
select top (10)/top (10) percent * from xxiang
top (10) 是显示前10条记录
top (10) percent 是显示这个表的所行数据的前10%记录
select top (2) with ties * from xxiang where id = 10
with ties在这里是起到与第二条记录一样的后面的记录也会被显示
示例 假如有3条记示符这个要求
id name
10 xxxx
10 sdf
10 sfgg
where 关键字
where 后面可跟比较运算符(>,>=,<,<=,<>)
select * from xxiang where id>10
where后面可跟like关键字
like后可跟四种通配符%,_,[],[^]
%表示零个、一个或多个任意字符
_表示一个任意字符
[]表示这个范围内可取任意一个任意字符
[^]表示这个范围内不可取任意一个任意字符
select * from xxiang where name like '%xx%'
select * from xxiang where name like '[xc]ing%' //x或c开头以ing结尾的字符串
逻辑运算符 AND,OR,NOt