0

I have the following query that works great except it is too restrictive:

SELECT sr.AssignedTech
     , i.image 
  FROM ScheduleRequest sr
     , images i
 WHERE JobStatus != 0 
   AND ScheduleDateExact IS NOT NULL 
   AND ID IS NOT NULL 
   AND RecordType != 2 
   AND JobStatus != 1 
   AND sr.AssignedTech = i.empno

If there is no match between ScheduleRequest.AssignedTech=images.empno then the record is not selected.

How do I display records even where this specific condition is not met? I tried to change it to OR but that did not work.

4
  • create a second query that's the opposite of the conditions? Commented Jun 7, 2018 at 14:05
  • @ThisGuyHasTwoThumbs i'd even say remove the condition... Commented Jun 7, 2018 at 14:08
  • We stopped writing queries this way ca. 1995. Use explicit JOIN syntax instead. Then switch from your existing INNER JOIN to a [LEFT] OUTER JOIN, and move all the conditions on the joined table from the WHERE clause to the ON clause. Job done. Commented Jun 7, 2018 at 14:08
  • I need the sr.AssignedTech = i.empno if it exists. Commented Jun 7, 2018 at 14:16

2 Answers 2

1

You can use a left join in order to get all the AssignedTech records including their image if they have one:

SELECT sr.AssignedTech
     , i.image 
  FROM ScheduleRequest sr
LEFT JOIN images i on (sr.AssignedTech = i.empno)
 WHERE JobStatus != 0 
   AND ScheduleDateExact IS NOT NULL 
   AND ID IS NOT NULL 
   AND RecordType != 2 
   AND JobStatus != 1
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using an Inner join, may be you could use outer joins to fetch the data . if you need to fetch the whole data from the left table and only the rows that satisfy the condition from the right table, you could use left outer join. and vice versa from the right outer join. If you need everything from both the tables you could use full outer join. Hope this information is helpful. :)

Comments

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.