Two tables with same field names >> student.id in student table (a) and student.id in record table (b)
If student.id from student table is also in record table (join on a.student.id = b.student.id) then say "Yes" else "No" as "match"
If there are no duplicates, then you can use a left join
:
select s.*, (case when r.student_id is null then 'No' else 'Yes' end)
from students s left join
records r
on r.student_id = s.id;
If there can be duplicates, then remove the duplicates before joining:
select s.*, (case when r.student_id is null then 'No' else 'Yes' end)
from students s left join
(select distinct r.student_id
from records r
) r
on r.student_id = s.id;
Consider a LEFT JOIN which will show all the records from the first table, plus any matching records from the second table. You could use a CASE statement to display whether they match.
SELECT
A.StudentID,
B.StudentID,
CASE WHEN b.StudentID IS NOT NULL THEN 'Yes' ELSE 'NO' END As Match
FROM
StudentA a
LEFT JOIN StudentB b on a.StudentID = b.StudentID
you could use a left join and a case when
select case when b.student_id is null then 'No' else 'Yes' END match
from student a
left join record b on a.student_id = b.student_id
The following is safe from duplicates and probably faster if there are many duplicates.
select *,
case when exists (select null from other_students b
where b.id = a.id) then 'Yes' else 'No' end student_match
from students a
See the SQL Fiddle in action here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.