0

i have two tables timetable and subject and i need id,subject_name from subject table which is not used in timetable for specific section.

timetable table timetable subject table

subject

but it is returning multiple values as you can see in image below. result

I am using this query

SELECT subject.id, subject.subject_name 
FROM timetable 
LEFT JOIN subject 
ON timetable.subject_id != subject.id AND timetable.class_id = subject.class_id 
WHERE timetable.class_id = 1 and timetable.section_id =1
jimmu
  • 1,025
  • 1
  • 10
  • 15

2 Answers2

1

I still think it's a duplicate question. Though your issue is a bit more complex. So try this one:

SELECT subject.id, subject.subject_name 
FROM subject
LEFT JOIN timetable 
  ON  timetable.subject_id = subject.id
  AND timetable.class_id   = subject.class_id 
  AND timetable.section_id = 1
WHERE subject.class_id = 1
  AND timetable.id IS NULL
Paul Spiegel
  • 30,925
  • 5
  • 44
  • 53
  • This worked... i also made a query which also works SELECT id, subject_name FROM subject WHERE class_id = 1 AND id NOT IN (SELECT subject_id from timetable where section_id=1) – Syed Sarmad Ali Jan 28 '18 at 21:13
0

This simple query worked.

SELECT id, subject_name FROM subject WHERE class_id = 1 
AND id NOT IN (SELECT subject_id from timetable where section_id=1)