【问题标题】:How to apply Self Join如何申请自我加入
【发布时间】:2017-04-15 15:11:10
【问题描述】:

架构:

学生(snum:整数,sname:char(30),major:char(25),level:char(2),age:整数)

教师(fid:整数,fname:char(30),deptid:整数)

类 (cname: char(40), meet_at: char(20), room: char(10), fid: integer | fid REFS 教员.fid)

已注册 (snum: integer, cname: char(40) | snum REFS student.snum, cname REFS 类名)

我想打印所有级别的学生的级别和年龄 除了“JR”。我知道我可以以简单的方式应用此查询。但我想加入我们的行列

我的尝试:

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');

这没有给我预期的答案。我是不是做错了什么?

【问题讨论】:

  • 你不想join ?!!
  • 我想通过JOINS来做这个
  • 喜欢这个Select s.level, s.age from student s where s.level != 'JR' ??
  • 我认为您没有使用联接。您正在使用子查询。
  • 你是什么意思,这没有给你预期的答案?请用输入输出说明。

标签: mysql join self-join


【解决方案1】:

实际上在这种情况下进行子查询或连接没有任何意义,而您可以通过简单的查询来获得它。

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

它应该会给你想要的输出。

【讨论】:

  • "," 是 "cross join" 的另一种拼写,它的绑定不如所有 "join" 运算符。
【解决方案2】:

虽然我同意 join 可能有点矫枉过正,但这看起来像是家庭作业,在这种情况下,您可能被要求专门使用 join。这是我的尝试:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多