SQL:三个表的连接

时间:2014-01-18 13:18:36

标签: sql sql-server

我是SQL的新手,现在正在学习表的连接。

我被困在加入三张桌子。

(我已经将已插入表格的行也提供给您参考。)

我的桌子是

--Table1

    create table sql_students(
    stu_studentid int not null primary key,
    stu_name varchar(100) not null,
    stu_regnnumber bigint unique not null
    )

--Rows inserted to Table1

insert into sql_students (stu_studentid,stu_name,stu_regnnumber) values (1,'John',194300)
insert into sql_students (stu_studentid,stu_name,stu_regnnumber) values (2,'Joy',959595)
insert into sql_students (stu_studentid,stu_name,stu_regnnumber) values (3,'Lucy',474848)

--Table2

create table sql_exam(
exa_examid bigint not null primary key,
exa_name varchar(100) not null,
exa_maxmark decimal(5,2) not null,
exa_minmarkreqdforpass decimal(5,2) not null,
exa_examscheduletime datetime not null
)

--Rows inserted into Table2

insert into sql_exam(exa_examid,exa_name,exa_maxmark,exa_minmarkreqdforpass,exa_examscheduletime) values (1,'Maths',100,40,'2012-10-10 10:00')
insert into sql_exam(exa_examid,exa_name,exa_maxmark,exa_minmarkreqdforpass,exa_examscheduletime) values (2,'English',75,35,'2012-10-11 10:00')

--Table3

create table sql_studentmarks(
stm_studentid int foreign key references sql_students(stu_studentid),
stm_examid bigint foreign key references sql_exam(exa_examid),
stm_mark decimal(5,2)
)

--Rows inserted into Table3
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (1,1,80)
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (2,1,90)
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (3,1,40)
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (1,2,70)
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (2,2,60)
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (3,2,17)

我需要指南才能获得

  1. 所有通过考试的学生
  2. 所有参加所有考试的学生
  3. 所有学生及其在数学和英语方面的差异。
  4. 提前致谢。

2 个答案:

答案 0 :(得分:2)

#1

SELECT s.stu_name 
FROM sql_studentmarks AS m 
JOIN sql_students AS s ON m.stm_studentid = s.student_id 
JOIN sql_exam AS x ON COUNT(m.stm_examid) = COUNT (x.stm_examid)
WHERE m.stm_mark >= x.exa_minmarkreqdforpass GROUP BY s.stu_studentid;

对于#2,你可以根据考试计数,也许在select语句中创建一个var:

SELECT s.stu_name 
FROM sql_studentmarks AS m 
JOIN sql_students AS s ON m.stm_studentid = s.stu_studentid 
RIGHT OUTER JOIN sql_exam AS x ON COUNT(m.stm_examid) = COUNT (x.stm_examid)
AND m.stm_examid IS NOT NULL
GROUP BY s.stu_student_id;

FOR#3使用上面的示例,简单连接将执行:

SELECT s.stu_name, x.exa_name, m.stm_mark 
FROM sql_studentmarks AS m 
JOIN sql_students AS s ON m.stm_studentid = s.stu_studentid 
JOIN sql_exam x ON  m.stm_examid = x.exa_examid AND s.stu_studentid = x.exa_studentid;

答案 1 :(得分:1)

下面的代码显示了前两个查询。您的联接是正常的..在SQL Fiddle中尝试这些,然后看看您是否可以处理第三个查询

查询#1 :(你非常接近,只需要distinct关键字)

select  stm_studentid,stu_name  
from sql_exam 
join sql_studentmarks on exa_examid=stm_examid 
                         and stm_mark>exa_minmarkreqdforpass 
inner join sql_students on stu_studentid=stm_studentid
group by stm_studentid,stu_name
having count(*) = (select count(*) from sql_exam)

查询#2: -

  • 通过分组,您可以计算参加考试的次数。
  • 允许您将计数与可用考试数量进行比较
select stu_name,count(*) as NumExamsTaken
from sql_studentmarks
join sql_students on stu_studentid=stm_studentid
group by stu_name
having count(*) = (select count(*) from sql_exam)

查询#3:
这应该让你开始

 select  stu_studentid,stu_name,
            MG.stm_mark as MathGrade,
            EG.Stm_mark as EnglishGrade
    from sql_students
    join sql_exam MATH on MATH.exa_name='MATHS'
    join sql_exam ENG on ENG.exa_name='ENGLISH'
    join sql_studentmarks MG on MG.stm_studentid=stu_studentid
                            and MG.stm_examid=MATH.exa_examId
    join sql_studentmarks EG on EG.stm_studentid=stu_studentid
                            and EG.stm_examid=ENG.exa_examId