2

I have been looking at other solutions on here for this but they seem to specify on normal queries and not joins, unless I'm missing something.

I have the following mysqli query in a function (it loads a job and all the details that go with it)

$sql = "
SELECT j.*
     , p.*
     , i.*
     , s.*
     , t.* 
  FROM jobs j
  JOIN job_priority p
    ON p.priority_id = j.priority_id 
  JOIN it_staff i
    ON i.staff_id = j.assigned_to  
  JOIN job_status s
    ON s.status_id = j.status_id 
  JOIN tasks t
    ON t.job_id = j.job_id 
 WHERE j.job_id = '$job_id'
";

Now this query works fine but only if there is a value in the tasks table assigned to the corresponding job_id, but if there isnt any tasks assigned to the job then the result will come up blank. I understand WHY this is the case but what I want to do is select all the data and return it even if there are no tasks.

I have looked at IS NOT NULL and a few other solutions but I'm not sure on the correct syntax for it when using a join query like above.

So I am seeking the syntax to only select the tasks if they exist (the tasks and jobs table are linked/joined by the job_id column).

halfer
  • 19,824
  • 17
  • 99
  • 186
Mike Abineri
  • 409
  • 2
  • 13
  • 1
    Do you know what a LEFT JOIN does? – LukStorms Nov 11 '19 at 15:58
  • Vaguely, I have always used inner joins and never really used the other join methods. I take it this is where i should be looking! @LukStorms can I use a left join in combination with my inner joins all in the same query ? – Mike Abineri Nov 11 '19 at 16:00
  • you just need a left join instead of your inner join – Your Common Sense Nov 11 '19 at 16:01
  • 1
    Yes, one can use different types of joins in the same query. – LukStorms Nov 11 '19 at 16:03
  • @LukStorms your a diamond! Thank you very much and sorry everyone for the duplicate question ! – Mike Abineri Nov 11 '19 at 16:04
  • Well this question got my account banned from asking more questions for 6 months which seems pretty harsh, I get the rules are there to keep the quality of the site but I clearly didnt find or understand any of the answers I did come across hence why I asked. Seems a bit over the top to ban me whoever did. I will use this comment to ask one last question before i just give up on this platform all-together Why do people short hand their tables to single letters? is there any benefit to this as i my opinion it looks more messy/complicated? @LukStorms – Mike Abineri Nov 11 '19 at 16:11
  • 1
    It's an automated process I think, so there noone to look at with angry eyes. But yeah, that sound harsh indeed. And I had no idea duplicate questions could also cause that. About the short alias names, it's often more readable than using the full table names. – LukStorms Nov 11 '19 at 16:19
  • @LukStorms I wish you where an admin to be honest mate! thanks for your help dude. – Mike Abineri Nov 11 '19 at 16:20
  • @LukStorms Someone upvoted the question and not I can post questions again! if it was you then thank you very much! you saved my account :D – Mike Abineri Nov 11 '19 at 16:23
  • 1
    I had no idea a closure will impose a ban. A short letter comes in handy when you have a lot of fields to select and/or conditions. – Your Common Sense Nov 11 '19 at 17:04

0 Answers0