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
3

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

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)
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)

1 Comment

the distinct is not needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.