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

sql server 基本操作二

时间:2016-02-05 18:35:53      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

基本查询

create database ZK_Test
go

use zk_test
go

create table Student
(
    ID int identity(1,1) primary key,
    Name varchar(20) not null,
    Sex char(2) not null,
    Birthday datetime, 
    Height int
)

select * from student

insert into student (Name, Sex, Birthday, Height)
values
(
    张三,,1988-08-12, 172
),
(
    张三1,,1986-08-12, 168
),
(
    我就,,1988-08-12, 122
),
(
    kk,,1988-12-19, 132
),
(
    吗看,,1958-9-12, 152
),
(
    咯跑,,1976-8-12, 102
),
(
    用额,,1988-8-12, 192
),
(
    离开,,1988-8-1, 152
)

select distinct * from student

select top 3 * from student

select top 30 percent * from student

declare @count int = 5
select top (@count) * from student 

select Name as 姓名 from student

select Name + zk as 姓名 from student

select Name + zk + convert(varchar(20), birthday, 112) as Name from student

select datediff(year, birthday, getdate()) age from student

--group by操作用by的字段生成了临时表,在其后进行的操作
--只能用临时表有的字段,否则需要使用max、min等对原表进行操作组合成新表
select 
name,max(sex) as sex,max(height) as height
from student 
where height >= 151
group by name
having max(sex) = 
order by name desc

select * from student where height = 172

select * from student where sex = 

select * from student where height >= 150 and height <= 175

select * from student where height between 150 and 178

select * from student where height in(176, 172, 180)

select * from student where height not in(176, 172, 180)

select * from student where height like 1_2

select * from student where height like 17%

select * from student where height like 1[67][012345]

select * from student where height is null

update student set height = null where id = 8

select * from student where height is not null

--将null值转换为0
select * from student where isnull(height, 0) < 180 

select * from student where exists
(
    select 1
)
--判断某个数据是否有
if exists
(
    select * from student where id=12
)
print exist
else
print not exists

select * from student where exists
(
    select * from student where id=9
)

select * from student as t1 where exists
(
    select * from student where id = t1.id and height > 173
)


select * from student where charindex(, name, 1) >= 1

select * from student where patindex(张_, name) >= 1

select * from student order by name desc

alter table student add class int default(null)

--循环赋值
declare @no int
set @no =1

while @no < 30
    begin
        update student set class =@no where id=@no
        set @no = @no + 1
    end


select * from student order by sex,class

select class,year(birthday),count(id),avg(height),max(birthday) from 
student group by class,birthday

select class,year(birthday),count(id),avg(height),max(birthday) from 
student group by class,birthday having class=1

select class,year(birthday),count(id),avg(height),max(birthday) from 
student group by class,birthday having avg(height) > 165

select class,sum(isnull(height,0)) from student group by class

select class,avg(height) from student group by class

select class,count(height) from student group by class

select class,max(height) from student group by class

select class,min(height) from student group by class

select class,count(distinct(height)) from student group by class

 

sql server 基本操作二

标签:

原文地址:http://www.cnblogs.com/zkzk945/p/5183043.html

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