1.我现在要查找students和student_lesson这二个数据库里面studentno字段有没有一样的,有没有符合的
select a.studentno, a.studentname, a.age, a.sex from [dbo].[Students] a -- 起别名,这里a就代表了这张表
where exists (select * from [dbo].[student_lesson] b where a.studentno = b.StudentNo);
-- 全查出来b表的数据 起别名 a的studentno字段 等于b的studentno的字段
出来三笔符合的数据,说明二张表存在则显示studentno的字段
2.相反,举一反三 加个not 就是说不存在则显示的数据
select a.studentno, a.studentname, a.age, a.sex from [dbo].[Students] a
where not exists (select * from [dbo].[student_lesson] b where a.studentno = b.StudentNo);
出来了四个