加入db表

时间:2017-03-13 05:03:14

标签: java mysql sql oracle

我有2个数据库表:

  • 课程

    id|name|teacher_id
    
  • 教师

    id|first_name|last_name|email
    

我想在id, name表格中显示courses,在first_name, last_name表格中显示teachers

我使用full join method

select name, 
    first_name, 
    last_name 
from courses 
full join teachers on teachers.id = courses.teacher_id;

我收到以下错误:

  

未知栏' courses.teachers_id'在' on条款'

2 个答案:

答案 0 :(得分:0)

试试这个:

select name, first_name, last_name from courses 
inner  join 
teachers on teachers.id = courses.teacher_id;

答案 1 :(得分:0)

您在MySQL上没有FULL JOINS,但实现如下:

如果您打算 FULL OUTER JOIN

select name, first_name, last_name from courses left join teachers on teachers.id = courses.teacher_id;
union all
select name, first_name, last_name from courses right join teachers on teachers.id = courses.teacher_id;

INNER JOIN

select name, first_name, last_name from courses left join teachers on teachers.id = courses.teacher_id;