简体   繁体   English

如何申请自助加入

[英]How to apply Self Join

Schema: 架构:

Student (snum: integer, sname: char(30), major: char(25), level: char(2), age: integer) 学生(人数:整数,姓名:char(30),专业:char(25),级别:char(2),年龄:整数)

Faculty (fid: integer, fname: char(30), deptid: integer) 教师(fid:整数,fname:字符(30),deptid:整数)

Class (cname: char(40), meets_at: char(20), room: char(10), fid: integer | fid REFS Faculty.fid) 类(名称:char(40),Meets_at:char(20),房间:char(10),fid:整数| fid REFS Faculty.fid)

Enrolled (snum: integer, cname: char(40) | snum REFS student.snum, cname REFS class.name) 已注册(snum:整数,cname:char(40)| snum REFS student.snum,cname REFS class.name)

I want to print the level and the age of students for all levels except 'JR'. 我想打印除“ JR”以外所有级别的学生的水平和年龄。 I know I can apply this query in a simple way. 我知道我可以以一种简单的方式应用此查询。 But I want to us e JOINS 但是我要我们加入

My attempt: 我的尝试:

select s.levels as Seniority,s.age as Age

from student s

where s.levels not in (

select a.levels

from student a

where a.levels='JR');

This is not giving me expected answer. 这没有给我期望的答案。 Am I doing some mistake? 我在做错什么吗?

Actually it doesn't make any sense to do sub query or join in this case while you can get it with simple query like. 实际上,在这种情况下进行子查询或联接没有任何意义,尽管您可以使用简单的查询来获得它。

SELECT level as Seniority, age as Age from student WHERE levels != 'JR'

It should give you desired output. 它应该给您所需的输出。

While I agree a join may be overkill, this looks like homework, in which case you may have been asked to specifically use a join . 尽管我同意join可能会显得过高,但这看起来就像是作业,在这种情况下,可能会要求您专门使用join Here's my attempt: 这是我的尝试:

select distinct levels as Seniority, age as Age
  from student
       natural join
       ( select *
           from student a
          where a.levels != 'JR' );

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

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