Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL join with Where clause still displays part of record

I have read this question as it seemed similar but it does not quite work for my scenario and I have not been able to adapt this solution to my problem:
Left Join With Where Clause

My SQL query currently includes a join on the JOBS table on the column production_id like this:

SELECT md.production_id,manufacture_id, pr.product_code,j.production_id as jobs,j.job_type
FROM MANUFACTURE_DATE md
INNER JOIN PRODUCTS pr
on md.production_id = pr.production_id
LEFT JOIN JOBS j
on md.production_id = j.production_id
WHERE active='1' AND date='2021-09-09'
ORDER BY product_code DESC;

This returns a product_code which has a record in the jobs table with a job_type of 3

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

On the join with the JOBS table I now only want to include values with an job_type=1 but not job_type=2 or job_type=3 so I have included a WHERE clause in the join as below:

SELECT md.production_id,manufacture_id, pr.product_code,j.production_id as jobs,j.job_type
FROM MANUFACTURE_DATE md
INNER JOIN PRODUCTS pr
on md.production_id = pr.production_id
LEFT JOIN JOBS j
on md.production_id = j.production_id AND j.job_type=1
WHERE active='1' AND date='2021-09-09'
ORDER BY product_code DESC;

This returns the product_code which has a record in the jobs table and displays NULL for jobs.production_id and jobs.job_type

My aim is to not have this product_code displayed at all because it is present in the JOBS table but not with the desired job_type of 1.

>Solution :

The solution for your problem can be in two ways:

  1. Using Inner Join instead of Left Join (Assuming all production_ids present in Manufacture Table are also present in Jobs Table)

     SELECT md.production_id,manufacture_id,
     pr.product_code,jobs.production_id as jobs,jobs.job_type
     FROM MANUFACTURE_DATE md 
     INNER JOIN PRODUCTS pr 
     ON md.production_id = pr.production_id 
     INNER JOIN JOBS jobs 
     ON md.production_id = obs.production_id AND jobs.job_type=1 
     WHERE active='1' AND date='2021-09-09' ORDER BY product_code DESC;
    
  2. Using same filter condition on job_type but in where clause along with one more condition:

     SELECT md.production_id,manufacture_id,
     pr.product_code,jobs.production_id as jobs,jobs.job_type
     FROM MANUFACTURE_DATE md
     INNER JOIN PRODUCTS pr
     on md.production_id = pr.production_id
     LEFT JOIN JOBS jobs
     on md.production_id = jobs.production_id 
     WHERE active='1' AND date='2021-09-09' 
     AND (jobs.job_type=1 OR jobs.production_id IS NULL)
     ORDER BY product_code DESC;
    

Why you are going wrong can be explained through sample code example given at below link:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=876eb66e340e27b05ae543238b5a88ff

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading