简体   繁体   English

在Oracle11g SQL中联接三个表

[英]Joining three tables in Oracle11g SQL

Normally, this wouldn't be a problem for me, but 1. My instructor never taught us how to join three tables like this and 2. It wasn't even covered on this week's assignment. 通常,这对我来说不是问题,但是1.我的教练从未教过我们如何联接三个这样的表和2.甚至在本周的作业中也没有涉及。 But because these databases aren't in Oracle interface database that we normally use, I have no way of even doing trial and error. 但是因为这些数据库不在我们通常使用的Oracle接口数据库中,所以我什至无法进行反复试验。

I have three tables. 我有三张桌子。 The contents aren't important to the question. 内容对这个问题并不重要。 The first table is STUDENTS, with columns LAST_NAME, SID, and MID (two of the MID cells have NULL values here). 第一个表是STUDENTS,具有LAST_NAME,SID和MID列(此处两个MID单元具有NULL值)。 Second table is COURSES, with columns COURSES (don't know why whoever designed this question decided to name it the same as the table) and CID (I'm assuming that's a primary key) Third table is ENROLLED, with columns SID and CID. 第二个表是COURSES,其列为COURSES(不知道为什么设计此问题的人决定将其命名为与表相同)和CID(我假设这是主键)。第三个表是ENROLLED,其列为SID和CID 。

I'm asked the following question "Use the three tables shown above to answer the following questions: a. What kind of join would you use to join all three tables? Write the syntax that would produce the desired result. b. Name two tables that could be used to retrieve data from a natural join. Write the syntax that would produce the desired result." 我被问到以下问题“使用上面显示的三个表来回答以下问题:a。您将使用哪种联接来联接所有三个表?编写将产生所需结果的语法。b。命名两个表可以用来从自然联接中检索数据。编写可产生所需结果的语法。”

I'm stuck on this one. 我被困在这一个。 Internet hasn't been much help so far, not even Oracle's materials. 到目前为止,Internet并没有太大帮助,甚至Oracle的资料也没有。 We learned about joining, but we never joined more than two tables. 我们了解了联接的知识,但从未联接两个以上的表。 Any help would be appreciated. 任何帮助,将不胜感激。

STUDENTS, with columns LAST_NAME, SID, and MID COURSES, with columns COURSES and CID ENROLLED, with columns SID and CID. 具有LAST_NAME,SID和MID COURSES列,具有COURSES和CID ENROLLED列,具有SID和CID列的学生。

a table can be joined with another table if both the table have one column in common. 如果两个表都有一个共同的列,则一个表可以与另一个表连接。 so here STUDENTS TABLE can be joined with ENROLLED, 所以这里的学生表可以和ENROLLED一起使用,

ENROLLED.SID =STUDENTS.SID ENROLLED.SID =学生.SID

and COURSES can be joined with ENROLLED 和课程可以与ENROLLED一起加入

ENROLLED.CID=COURSES.CID ENROLLED.CID =课程.CID

What kind of join would you use to join all three tables? 您将使用哪种联接来联接所有三个表? Write the syntax that would produce the desired result. 编写将产生所需结果的语法。

select last_name, courses
  from students 
    left join enrolled on students.sid = enrolled.sid
    left join courses on enrolled.cid = courses.cid

or 要么

select last_name, courses from students
  left join (select * from enrolled join courses using (cid)) using (sid)

SQLFiddle SQLFiddle

Here I used left join to show student even if he did not enroll anywhere. 在这里,我使用左加入来显示学生,即使他没有在任何地方注册。 But you can use also inner join(s) to avoid such situation or full join to show everything - students who enrolled somewhere, these who did not enroll and courses that have no students. 但是,您也可以使用内部联接来避免这种情况,也可以使用完全联接来显示所有内容-在某个地方就读的学生,未就读的学生和没有学生的课程。 In this Stack Overflow topic you have very good explanation how different types of joins works. 在这个Stack Overflow主题中,您将很好地解释不同类型的联接如何工作。

Name two tables that could be used to retrieve data from a natural join. 命名两个表,这些表可用于从自然联接中检索数据。 Write the syntax that would produce the desired result. 编写将产生所需结果的语法。

These pairs are: ENROLLED and STUDENTS (common key SID) and ENROLLED and COURSES (common key CID). 这些对是:ENROLLED和STUDENTS(公共密钥SID)以及ENROLLED和COURSES(公共密钥CID)。

select * from enrolled natural join students;

select * from enrolled natural join courses;

You can use SQLFiddle to test your own ideas, like I used for first query. 您可以使用SQLFiddle测试您自己的想法,就像我第一次查询时所用的那样。 Choose database (Oracle) from listbox, next build schema (create tables, insert data) in left panel then write queries in right panel. 从列表框中选择数据库(Oracle),左侧面板中的下一个构建模式(创建表,插入数据),然后在右侧面板中编写查询。 If query runs succesfully results will be visible at bottom. 如果查询成功运行,结果将显示在底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM