注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 科普系列之-使用Windows的..
 帮助

SQL SERVER2000教程-第五章 处理数据 第二节 检索数据


2007-10-01 13:22:15
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://chenxing.blog.51cto.com/240526/44590
SQL SERVER2000教程-第五章 处理数据 第二节 检索数据
是数据库最频繁执行的活动。在SQL中,使用SELECT语句可以在需要的表单中检索数据。
格式:Select [All | Distinct ] 字段表列 from 表名
         [Where 查询条件]
         [Group by 字段表列(分组)]
         [Having 分组条件(用于已分组的结果)]
         [Order by 字段表列 [Asc(升序)| Desc(降序)]]
实例: Use student
        go
        Create table student (stuno int primary key ,stuname varchar(10) not null,
                         Class int , sex char(2) check (sex in (‘男’,’女’)))
        插入值:
        insert into student values(1,'tom',5,'男')
        insert into student values(2,'rose’,6,'女')
        insert into student values(3,'smith',6,'男')
        insert into student values(4,'mary',5,'女')
1、基本语句检索数据
   a)全表查询:Select * from student
   b)选择字段查询:Select stuno ,stuname from student
   c)排列数据:Select stuname,class from student order by stuno desc
   d)消除重复项:Select class from student | Select distinct class from student
   e)约束结果:Select stuname from student where stuno=1
2、选择语句检索数据
(1)比较运算符:
     <   小于  >  大于 <=  小于等于 >=  大于等于 <> 或!=  不等于
     实例:Select stuname from student where class <>2
(2)between……and …… 或 not between……and …… 运算符:
      between 后是数值的下限,and 后是数值的上限。between……and ……包括上下限。
      not between……and ……不包括上下限。
      实例:Select * from student where stuno between 2 and 4
(3)In 运算符:查找属性值属于指定集合的元组。
     实例:Select * from student where stuno in(1,3,4)
(4)like 运算符:属于字符串匹配条件查询。有两个通配符:
     a)‘_’ (下划线):代表任意单个字符。
        实例:Select * from student where stuname like ‘t_m’
     b)  %(百分号):代表任意长度的字符串。
        实例:Select * from student where stuname like ‘%m’
(5)is [not]null 运算符(未知值):
        实例:Select * from student where class is not null
(6)and或者or运算符:多重条件查询。
       实例:Select * from student where stuno=1 or stuno=2 or stuno=3
            Select * from student where stuno=1 and stuname=’tom’




    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: