Sql Server——查询(一)

  查询数据就是对数据库中的数据进行筛选显示。显示数据的表格只是一个“虚拟表”。

  查询

     (1)对列的筛选:

            1、查询表中所有数据: select * from 表名                  这里的 *  表示所有列的意思

            2、查询表中指定列的数据:select 列名1,列名2,列名n from 表名             注意:列名和列名之间用半角逗号隔开,最后一个不用逗号,因为这里只是对指定列的查询,所以就不用 * 号了。

            3、在查询数据的时候,我们还可以对列名进行更改(如以中文显示),取别名,但取别名并不影响原表中的内容,因为查询的内容只是在临时生成的虚拟表中显示的。 一共有三种写法,但一般推崇使用第一种。

              一:应用as关键字        select 列名1 as 别名1,列名2 as 别名2, 列名n as 别名n  from 表名              同样的要注意逗号和 * 号

              二:应用空格    就是把上面的as 改为空格     select 列名1 空格 别名1,列名2 空格 别名2, 列名n 空格 别名n  from 表名

              三:应用 = 号   select 别名1=列名1,别名2=列名2,别名n=列名n    这里要注意列名和别名的位置

     (2)对行的筛选:

            1、查询固定的行数的数据: select top 行数 * from 表名      * 指所有列,如要只查询其中的几列把 * 改为列名就可以了,这里的行数是从上往下的

            2、按百分比查询数据:select top 百分比 percent * from 表名  这里的 * 号和上面一个一样   percent表示 %  如果遇到单行数据,查询时就是四舍五入

     (3)排序:

            利用关键字  order by

            select * from 表名 order by 列名   asc/desc    asc(升序,也是默认方式)  desc(降序)

     (4)条件查询(即where子句):

            1、select * from 表名 where 条件

            2、between.......and....(范围查询): select * from 表名 where 列名 between 范围一(10)  and 范围二(20)   (如查询年龄10-20之间的)

            3、in(等值查询): select * from 表名 where 列名 in(值一,值二,值n)

            4、is(空值查询):select * from 表名 where 列名 is null    -->查询空值      select * from 表名 where 列名 is not null       -->非空查询

     (5)模糊查询:

             select * from 表名 where like '格式'

             这里需要用到通配符:

                       %:任意字符匹配   多个

                       _:单个字符匹配    单个

                       []:范围      [^]:范围之外

             比如查询姓张的:格式:张%     比如查询电话号码为010-1234567这种格式的: 格式:[0-9] [0-9] [0-9]-[0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]

  需要注意的是,以上查询都是可以相互配合使用的,具体见下面我做的作业代码:

  

create database Student
go

use Student
go

create table stuInfo
(
    sno ) not null primary key,
    name ) not null,
    sex ) not null check(sex='男' or sex='女'),
    birthday datetime not null check(birthday<=getdate()),
    phone ) ),
    address ) not null default('沙坪坝'),
    remark text
)
go

insert into stuInfo(sno,name,sex,birthday,phone,address)
    ',default)
insert into stuInfo(sno,name,sex,birthday,phone,address)
    ','大学城')
insert into stuInfo(sno,name,sex,birthday,phone,address)
    ','杨家坪')

create table course
(
    cno ,),
    cname ) not null
)
go

insert into course values('stb')
insert into course values('c')
insert into course values('html')
insert into course values('sql')
insert into course values('c#')

create table score
(
    no ,),
    sno ) not null references stuInfo(sno),
    cno int not null references course(cno),
    cj  )
)
go

,)
,)
,)
,)
,)
,)
,)

select * from stuInfo
select * from course
select * from score

--1、查询出所有姓张的学生信息
select * from stuInfo where name like '张%'
--2、查询出学号中包含3的,并且性别是男的所有学生信息
select * from stuInfo where sno like 'T[0-9][0-9]3[0-9][0-9][0-9]'
--3、查询出联系方式中最后3位号码是567的所有学生信息
select * from stuInfo where phone like '_______567_'      --题目有错
--4、统计考试及格的数据

--5、统计成绩在70-100之间的数据

--6、查询联系电话是44结尾的数据
select * from stuInfo where phone like '_________44'
--7、查询姓1号课程的成绩    

--8、查询张三的考试成绩
select * from score where sno='T123001'
--9、查询张三考试的最高分
 cj from score  order by cj desc
--10、查询张三的生日
select birthday  from stuInfo where name='张三'
--11、查询最后一名
 * from score order by cj asc
--12、查询地址为空的数据
select * from stuInfo where address is null
--13、查询电话号码以137开头的
select * from stuInfo where phone like '137%'
--14、查询家住沙坪坝的数据
select * from stuInfo where address='沙坪坝'
--15、查询性别为男的数据
select * from stuInfo where sex='男'
--16、查询成绩在60-80之间的(两种方式)

--17、查询成绩为60 70 100的数据( 两种方式)
,,)

--18、删除学生姓名为李四的数据(注意主外键)
delete score where sno='T123002'
delete stuInfo where sno='T123002'
--19、清空所有表中的数据(注意主外键)
delete score
delete course
delete stuInfo
--20、删除所有表(注意主外键)
drop table score
drop table course
drop table stuInfo

  

create database MyQQ
go

use MyQQ
go

create table Star
(
    Id ,),
    Star ) not null
)
go

insert into Star values('白羊座')
insert into Star values('金牛座')
insert into Star values('双子座')
insert into Star values('巨蟹座')
insert into Star values('狮子座')
insert into Star values('处女座')
insert into Star values('天秤座')
insert into Star values('天蝎座')
insert into Star values('射手座')
insert into Star values('摩羯座')
insert into Star values('水瓶座')
insert into Star values('双鱼座')

create table BloodType
(
    Id ,),
    BloodType ) not null
)
go

insert into BloodType values('A型')
insert into BloodType values('B型')
insert into BloodType values('O型')
insert into BloodType values('AB型')

create table Users
(
    Id ,),
    LoginPwd ) not null,
    NickName ) not null,
    Sex ) not null check(Sex='男' or Sex='女'),
    Age int not null,
    Name ),
    StarId int references Star(Id),
    BloodTypeId int references BloodType(Id)
)
go
select * from Users
--添加信息
,),
                        (',null,null,null),
                        (,),
                        (,),
                        (,),
                        (',null,null,null),
                        (,),
                        (',null,null,null),
                        (,),
                        (',null,null,null),
                        (,)

--查询出性别为“男”的所有用户信息
select * from Users where Sex='男'
--查询出年龄在20岁以下的所有用户信息

--查询出昵称为“benben”的密码
select LoginPwd from Users where NickName='benben'
--查询出表star中的所有信息
select * from Star
--查询出表BloodType中的所有信息
select * from BloodType
--查询出表Users中的所有信息,列名用中文显示
select ID as 编号, LoginPwd as 密码,NickName as 昵称, Sex as 性别, Age as 年龄, Name as 姓名, StarId as 星座名, BloodTypeId as 血型 from Users
--查询出年龄在10-50之间的所有用户信息

--查询出昵称为“豆豆”的血型名称,及星座名称
select StarId,BloodTypeId  from Users where NickName='豆豆'
--查询出所有用户的昵称、真实姓名、性别、星座编号,血型编号
 select NickName,Sex,Name,StarId,BloodTypeId  from Users
 --查询出密码不是0000的用户昵称
 '
 --查询血型编号为1的用户

 --查询用户表中所有数据
 select * from Users
 --查询星座表中的所有数据
 select * from Star
 --查询血型表中的数据
 select * from BloodType
 --查询最大年龄的昵称和真实姓名
  NickName,Name from Users order by Age desc
 --查询最小年龄的昵称和真实姓名
  NickName, Name from Users order by Age asc
 --删除表中所有数据(注意主外键)
 delete Users
 delete BloodType
 delete Star
 --删除所有表(注意主外键)
 drop table Users
 drop table BloodType
 drop table Star
上一篇:一个Silverlight工程的各文件解析


下一篇:用shape画内圆外方,形成一个圆形头像