日期:2014-05-18  浏览次数:20469 次

三表查询问题
Student(Sid,Sname) 学生表

infos(Iid,Sid,Sage,Ssex) 学生资料表

unions(uid,Sid,Uname) 学生会名单表

查询结果

学生姓名 课程名 学生会
  小明 数学 1
  小王 数学 1
  小张 物理 0


---------------------------------
以资料表为主表,查询所有学员的资料,并在结果中创建临时列(是学生会成员的为1,否则为0)

------解决方案--------------------
SQL code

select a.Sname 学生姓名,你的表示课程名的字段 课程名,
case when Uname isnull then 0 else 1 end as学生会
from 学生表 a left join 你的课程名所在的表 b
on a.sid=b.sid
left join unions c on a.sid=c.sid

------解决方案--------------------
SQL code

select a.Sname 学生姓名,b.Ssex 性别,
case when Uname isnull then 0 else 1 end as学生会
from 学生表 a left join infos b
on a.sid=b.sid
left join unions c on a.sid=c.sid

------解决方案--------------------
SQL code

select Sname,Ssex,(case when uid is null then 0 else 1 end) as 学生会
from Student a right join infos b on a.sid=b.sid left join unions c on a.sid=c.sid