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

SQLSever 第二堂课,主要学习内容为top查询前多少行,distinct去重,order by排序,group by分组,最重要子查询

时间:2015-04-20 16:21:22      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

create database xuesheng

drop database xusheng

use xuesheng

go

create table xueshengxinxi

 

(

code int primary key identity(1,1)not null,

name varchar(50) not null,

age int not null, sex varchar(50) not null,

hight int not null, [weight] int not null,

dizhi varchar(50 ) not null,

)

go

select *from xueshengxinxi

go

insert into xueshengxinxi values(‘张三‘,23,‘男‘,175,65,‘淄博‘)

insert into xueshengxinxi values(‘李四‘,24,‘男‘,170,50,‘潍坊‘)

insert into xueshengxinxi values(‘王五‘,25,‘男‘,165,62,‘临沂‘)

insert into xueshengxinxi values(‘赵六‘,26,‘女‘,162,60,‘济南‘)

insert into xueshengxinxi values(‘田七‘,27,‘女‘,172,63,‘德州‘)

insert into xueshengxinxi values(‘田七‘,20,‘女‘,160,58,‘临沂‘)

go

update xueshengxinxi set name=‘你好‘ where code=1--修改第一行name的名字,改成“你好”

update xueshengxinxi  set [weight]=50 where code=9

go

delete from xueshengxinxi --删除表中所有数据

delete from xueshengxinxi where age=4 --删除表中第四行数据

go select *from xueshengxinxi where code =4--查询表中第四行数据

select name,age from xueshengxinxi where code between 1 and 3--查询表中第一行到第三行name,age两列的 --具体信息

go delete from table xueshengxinxi --删除表中所有信息

delete from table where code=3 --删除表中第三行信息

drop table xueshengxinxi

--top 关键字

select top 3*from xueshengxinxi--查询前三行

select top 3 name,code from xueshengxinxi where age >=22--查询年龄大于22岁的前三行name列和code列的的信息

--distinct 去重

select distinct name from xueshengxinxi--把名字里重复的去掉

select distinct sex from xueshengxinxi--把性别去重, select disrinct sex=‘男‘ from xushengxinxi--把性别男的去重,不会再显示女的信息

--order by

select *from xueshengxinxi order by age asc --升序,按年龄排序

select *from xueshengxinxi order by hight desc--降序 ,按身高排序

select *from xueshengxinxi where age<25 order by age asc--把年龄小于25的查询出来并排序

select *from xueshengxinxi order by age asc,code desc--如果有两个年龄相同,先拍年龄再按照code 排倒序

select *from xueshengxinxi order by [weight] asc,code asc--如果有连个体重相同的,先按体重排正序 --再按学号排正序。

  order by+列名+asc是排正序,order by+列名+desc是排倒

 go

--分组

select age from xueshengxinxi group by age --对某一列进行分组,相当于去重显示,只显示一列

 select hight from xueshengxinxi group by hight --分组之后会自动排序

go

--子查询  !!!!!!!!!!!!! --使用查询语句查询,一一列数据出来,然后作为其他查询的查询条件中的参数来使用

--in 表示在什么参数之内

select *from xueshengxinxi where age in(23,24)--相当于age=23 or age=24。查询学生信息中23,24岁的信息

select *from xueshengxinxi where age not in(23,24)--查询学生信息中不是23,24岁的信息

go

--查询身高不在年龄是23岁的人身高范围之内的信息:

select *from xueshengxinxi where hight not in(select hight from xueshengxinxi where age=23) --先查询这个23岁的人,然后把这个23岁身高的信息去掉)

--名字叫田七的中的年龄比code=20的李四这个人的年龄小4岁的人的信息:

 select *from xueshengxinxi where age+4=(select age from xueshengxinxi where code=20) and name=‘田七‘

--名字叫田七中的比姓李的这个人的年龄大三岁的人的信息

: select *from xueshengxinxi  where age-3=(select age from xueshengxinxi where name like ‘李%‘) and name=‘田七‘

--比张三的身高矮的人的信息 select *from xueshengxinxi where hight<(select hight from xueshengxinxi where name =‘张三‘)

--比张三高的人比code=24矮的人信息 :

select*from xueshengxinxi where hight<(select hight from xueshengxinxi where name=‘张三‘) and hight>(select hight from xueshengxinxi where code=24) --一个女的比身高是170的那个人重8公斤的人的信息:

select*from xueshengxinxi where [weight]-8=(select [weight] from xueshengxinxi where hight=170) and sex=‘女‘

--一个比年龄是26的人小的女孩子的信息 :

select *from xueshengxinxi where  age<(select age from xueshengxinxi where age=26) and sex=‘女‘

--查询学生中身高是170和175的学生的信息:

select *from xueshengxinxi where hight in(170,175)

SQLSever 第二堂课,主要学习内容为top查询前多少行,distinct去重,order by排序,group by分组,最重要子查询

标签:

原文地址:http://www.cnblogs.com/275147378abc/p/4441641.html

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