SQL SERVER2008脚本运行所遇问题及解决方法2

 

 

---恢复内容开始---

哈哈哈 还是夜深人静撸代码让人开心~

1、连接查询操作时,只有列名唯一的情况(即此列名只在一个表中出现),select语句才省略表名,直接用select <列名>

2、可使用select语句一次插入多条语句

insert into sc(S#,C#,Score) select S#,02,(select AVG(SCORE) from sc where C#=02) from sc where c# != 01;
会插入的列:  

 

3.已知sc是自身存在的表,sc_2是不存在的表

(1)

update SC 
set score=(select AVG(sc_2.score) from Course,teacher,sc sc_2 where course.T#=teacher.T# and course.C#=sc_2.C# and sc_2.C#=SC.C# and teacher.Tname='' group by course.C#)
where existsselect from course,teacher,sc sc_2 where course.T#=teacher.T# and course.C# and sc_2.C#=sc.c# and teacher.Tname ='钱' group by course.c#);

代码运行步骤:

step1:使用where exists子句对course,teacher,sc_2进行连接后的符合条件(钱老师教的课)每一个元组进行辨别,挑选出有返回值的元组,进行下一步操作;

step2:再次对course,teacher,sc_2进行连接,针对代表钱老师教的课的元组进行列的选择。

结果正常

(2)

update SC 
set score=(select AVG(sc_2.score) from Course,teacher,sc sc_2 where course.T#=teacher.T# and course.C#=sc_2.C# and sc_2.C#=SC.C# and teacher.Tname='' group by course.C#);

代码运行步骤:

step1:对course,teacher,sc_2表进行连接,表中有的元组是关于钱老师的,有的不是关于钱老师的;

step2:对于不是关于钱老师的组,得到的平均分一定为空。最后的搜索结果是score列的一些元素变为空值。

(3)

update SC 
set score=(select AVG(sc_2.score) from Course,teacher,sc sc_2 where course.T#=teacher.T# and course.C#=sc_2.C# and teacher.Tname='' group by course.C#);

代码运行步骤:

step1: 运行正常

4.有子查询语句且当父查询使用聚集函数的时候,父查询必须配合group by使用.也就是说,select 子句中的列,除了 聚合函数 ,都要在 group by中出现。对于字符型的列,可在select语句中用max、min来代替聚集函数(见7中代码)

详情见 https://www.cnblogs.com/printN/p/6725026.html

5.NVL(E1, E2)的功能为:如果E1为NULL,则函数返回E2,否则返回E1本身。

6.case when then函数:

case expression 
when value1 then returnvalue1 else 
when value2 then returnvalue2
when value3 then returnvalue3
end

 7、如果在from语句中对表起了别名,则在语句中务必使用别名,否则会有警告“无法绑定有多个部分组成的通配符”,如下部分代码 max(course%)应为max(C%)

select T.C# as 课程号,MAX(course.cname)as 课程名,
    from sc T,course C
    where T.C#=C.C#
    group by T.C#;

8、函数

row_number() over (partition by column1 order by column2)

row_number是伪列名,按column1分区,按column2排序

9、from后面要加表

错误代码:

select* 
    from(select
            NB.S#,NB.score,(rank()over (PARTITION by nb.S# order by nb.score)) rk
            from sc nb 
            where nb.C#=01) 
    where rk<=2;

正确代码:

select* 
    from(select
            NB.S#,NB.score,(rank()over (PARTITION by nb.S# order by nb.score)) rk
            from sc nb 
            where nb.C#=01) as T1
    where T1.rk<=2;

 10、易错

1)查询各科成绩前三名的记录

select T1.S# 学生ID,T1.C# 课程ID,score as 分数
    from sc T1
    where score in(select top 3 score from sc where T1.C#=C# order by score desc)
    order by T1.c#;

结果:

2)查询总成绩前三名的记录

select T1.S# 学生ID,T1.C# 课程ID,score as 分数
    from sc T1
    where score in(select top 3 score from sc  order by score desc)
    order by T1.c#;

结果:

在第一例子中,每次父查询只向子查询传递一个元组

11、没有解答成功的错题

/*28*/
select (select COUNT(T2.S#) from T2 where Ssex='' group by T2.S#) 女生人数,
    (select COUNT(T2.S#) from T2 where Ssex='' group by T2.S#) 男生人数
    from Student,student T2
    where student.S#=T2.S#
    group by student.Ssex;
    
select Sum(case when ssex=''then 1 else 0) 男生人数
    sum(case when ssex=''then 1 else 0) 女生人数
    from student
    group by ssex;

第一套卷第28题 两种代码都错了

12、字符匹配用:like,not like

 

posted @ 2019-06-02 21:46  甜汤  阅读(267)  评论(0编辑  收藏  举报