代码改变世界

sql server 2008子查询的学习小纪(一)

2016-03-25 15:16  TNT_xxx  阅读(322)  评论(0编辑  收藏  举报

         对于sql server 2008 数据库的学习,笔者秉承一贯的 学习习惯,将 它做了整理,一便于后来学习时 能找准重点, 有不足之处 请大家 斧正,

      1.  对于任何一种数据库的学习, 主要 抓住对---- 这个概念的 “增(insert)”,“删(delete)”,“改(update)”,“查(select)” 这四个 功能, 而其中 尤其以 学习 “查(select)” 时需要的时间最长,  下面直接 进入对 子查询的  基础演示,

         先创建表:

create table student(

                   id int identity(1,1)primary key, 
                   name varchar(20),
                   age int not null,
)

create table score(

                   id int references student(id), 
                  score varchar(20),

                   classname varchar(20)
)

      -- 1.现在需要把 每个学生的 成绩 和 他链接起来

        简单查询 1. sql1=select name from  student

                     2.sql2=select score from score 

-- 链接起来

            select s1.name as 学生姓名, s2.score as 学生分数

                    from (sql1)s1,(sql2)s2 where s1.id=s2.id  

  --小结就是: 你要查的表缺什么 “列”,你就到 这个‘列’  所在的表把 列提取出来,

     或者 你还可以这样来做, 

  --   2.需要求出      有90分数的 学生的 id 和name

           select s1.name as 学生姓名, s1.id as 学生id

                     from (sql1)s1  where s1.id=(select id from score  where score =90 ) 

   -- 小结: 把 一个表中的 列的 一个 唯一的值 当成 条件,