SQL数据库问题,请教高手。

[复制链接]
查看11 | 回复4 | 2010-11-14 09:20:20 | 显示全部楼层 |阅读模式
--1.学生表
Student(S#,Sname,Sage,Ssex) --
2.课程表
Course(C#,Cname,T#)
--3.教师表
Teacher(T#,Tname)
--4.成绩表
SC(S#,C#,score) --S# 学生编号,C# 课程编号,score 分数
查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
--方法1
select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '01' and exists (Select 1 from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '02') order by Student.S#
--方法2
select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '02' and exists (Select 1 from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '01') order by Student.S#
--方法3
select m.* from Student m where S# in
(
select S# from
(
select distinct S# from SC where C# = '01'
union all
select distinct S# from SC where C# = '02'
) t group by S# having count(1) = 2
)
order by m.S#
这里面的1 (方法里的 Select 1 )是什么意思?

回复

使用道具 举报

千问 | 2010-11-14 09:20:20 | 显示全部楼层
count(*)和count(1)效果是一样的,统计null值,参考SQL:select count(*)和select count(1)的区别效果:两者的返回结果是一样的。意义:当count的参数是具体值时(如count(1),count('a')),count的参数已没有实际意义了。范围:在统计范围,count(*)和count(1) 一样,都包括对NULL的统计;
count(column) 是不包括NULL的统计。速度:表没有主键(Primary key),count(1)比count(*)快;
否则,主键作为count的参数时,count(主键)比co
回复

使用道具 举报

千问 | 2010-11-14 09:20:20 | 显示全部楼层
Select 1就是判断有没有结果返回,如果有结果返回就出现一个为1的查询结果值,没有结果则不显示。这里的1没有实际意义的,可以用任意值替换。
回复

使用道具 举报

千问 | 2010-11-14 09:20:20 | 显示全部楼层
Select 1 from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '01'是判断表SC中是否存在符合条件 SC_2.S# = SC.S# and SC_2.C# = '01'的行.如表:tableAname ageas
12da
234asd
3
回复

使用道具 举报

千问 | 2010-11-14 09:20:20 | 显示全部楼层
你的问题是?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行