选择两个表的差异
问题描述:
我正在尝试做一些我认为应该有点简单的事情。我有两个选择语句:选择两个表的差异
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;
它返回这个(模拟数据):
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;
然后返回t他:
我希望做的是显示三个记录出现在第一条语句,但不是在按字母顺序排列的第二条语句。我想基本上从语句1中减去语句2中的所有记录。有人可以帮助我吗?
答
尝试:
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
太谢谢你了!这工作完美。你能简单描述一下LEFT JOIN的作用吗? –