带有JOIN和WHERE子句的SQL SELECT - 从两个表中选择一条记录

时间:2018-01-03 17:00:17

标签: c# mysql sql

我有这样的事情:

&mut b'-' => {
    if let Some(next) = iter.peek() {
        if b'-' == **next {
            *ch = b' ';
            mode = Mode::COMMENT;
        }
    }
}

我需要获取隐藏在第一个或第二个表中的记录。我检查了,我将相同的数据插入第二个表。这个SELECT向我展示了常见的记录,但我只想要第一个或第二个表中的唯一记录。

1 个答案:

答案 0 :(得分:1)

根据您的评论,我修改了查询。以下是示例数据的两个示例:

<强>学生

  • Adam@abc.com
  • Bob@abc.com

<强>指导员

  • Bob@abc.com(注意您的数据结构确实允许这样做)
  • Chris@abc.com

<强>查询

/* Adam is a pupil but not an instructor */
select Pupil.*
from Pupil
left join Instructor on Pupil.email = Instructor.email
where Instructor.email is null
  and Pupil.email = 'Adam@abc.com'
union 
select Instructor.*
from Instructor
left join Pupil on Pupil.email = Instructor.email
where Pupil.email is null
  and Instructor.email = 'Adam@abc.com';

/* Chris is an instructor but not a pupil */
select Pupil.*
from Pupil
left join Instructor on Pupil.email = Instructor.email
where Instructor.email is null
  and Pupil.email = 'Chris@abc.com'
union 
select Instructor.*
from Instructor
left join Pupil on Pupil.email = Instructor.email
where Pupil.email is null
  and Instructor.email = 'Chris@abc.com';

您可以在http://sqlfiddle.com/#!9/c490c7/28

找到一个有效的例子