选择两个表的差异

时间:2016-12-01 23:45:15

标签: mysql select

我尝试做一些我觉得应该有点简单的事情。我有两个选择语句:

  1. SELECT concat(st.fname,' ',st.lname) as Fullname, st.parent_phone FROM student as st, grade as g, section as s, semester as sem WHERE st.studentid=g.studentid AND g.sectionid=s.sectionid AND s.semesterid=sem.semesterid AND s.semesterid not in (3, 4) Group by Fullname;
  2. 返回此(模拟数据):

    selectstatement1

    1. SELECT concat(st.fname,' ',st.lname) as Fullname, st.parent_phone FROM student as st, grade as g, section as s, semester as sem WHERE st.studentid=g.studentid AND g.sectionid=s.sectionid AND s.semesterid=sem.semesterid AND s.semesterid in (3, 4) Group by Fullname;
    2. 然后返回:

      enter image description here

      我想要做的是显示第一个语句中出现的三个记录,但不按字母顺序显示在第二个语句中。我想基本上从语句1中减去语句2中的所有记录。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

尝试:

SELECT x.*
FROM (
   -- the first subquery goes here
) x
LEFT JOIN (
   -- the second subquery goes here
) y
ON x.full_name = y.full_name AND x.parent_phone = y.parent_phone
WHERE y.parent_phone IS NULL
相关问题