I'm building a database for my work and I'm having trouble figuring out how to build this query.
The tables relevant to my problem are:
job
Surgical_Planning - has job as foreign key, exists for some jobs, doesn't for others
Medical_Model - has job as foreign key, 1 to 1 relationship with job
This is a working query where I don't have any information about the surgical planning
    SELECT
    job,
    physician_idphysician as Physician,
    patient_idpatient as Patient,
    status,
    DATE_FORMAT(scan_date, '%M %e, %Y, %l:%i%p') as Scan_Date,
    DATE_FORMAT(timestamp, '%M %e, %Y, %l:%i%p') as Recieved,
    DATE_FORMAT(date_required, '%M %e, %Y, %l:%i%p') as Date_Required
    FROM
    job, patient_has_physician as phys, Scan, Medical_Model as med
    WHERE
    Scan.job_job = job AND phys.job_job = job
    AND med.job_job = job AND job.type =  'medical
I think I want to do a Left join so that it will display every job in order, with all the information in the query above, but then when there is a Surgical_Planning for a job # I want there to be a column for that as well. Here is my attempt that is not working
    SELECT
    job,
    physician_idphysician as Physician,
    patient_idpatient as Patient,
    status,
    DATE_FORMAT(scan_date, '%M %e, %Y, %l:%i%p') as Scan_Date,
    DATE_FORMAT(timestamp, '%M %e, %Y, %l:%i%p') as Recieved,
    DATE_FORMAT(date_required, '%M %e, %Y, %l:%i%p') as Date_Required
    FROM
    job, patient_has_physician as phys, Scan, Medical_Model as med
    LEFT JOIN Surgical_Planning ON job.job = Surgical_Planning.job_job
    AND Scan.job_job = job AND phys.job_job = job
    AND med.job_job = job AND job.type = 'medical'
I can get this basic left join working the way I want, but if I want to add more columns like above it doesn't work.
    SELECT job, planning_id
    FROM job
    LEFT JOIN Surgical_Planning ON job = Surgical_Planning.job_job
could a subquery be used as well? I can figure out these more basic queries but really have trouble with these more complex join and subquery ones. any advice is appreciated.
EDIT --- Job table schema
-- Table mmrl.job
DROP TABLE IF EXISTS mmrl.job ;
CREATE  TABLE IF NOT EXISTS mmrl.job (
job INT(11) NOT NULL AUTO_INCREMENT ,
type VARCHAR(45) NULL ,
status VARCHAR(45) NULL DEFAULT NULL ,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
PRIMARY KEY (job) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
change
LEFT JOIN Surgical_Planning ON job.job = Surgical_Planning.job_job
AND Scan.job_job = job AND phys.job_job = job
AND med.job_job = job AND job.type = 'medical'
to
LEFT JOIN Surgical_Planning ON job.job = Surgical_Planning.job_job
WHERE Scan.job_job = job AND phys.job_job = job
AND med.job_job = job AND job.type = 'medical'
EDIT:
the left join occurs against the table to the left of the actual LEFT JOIN syntax.  Move job to the end of your from list and try again.
FROM patient_has_physician as phys, Scan, Medical_Model as med, job
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With