Jump to content

Trying to query 3 tables using INNER JOIN


Presto-X

Recommended Posts

Hello everyone,

 

I'm doing a simple query of 3 tables the first is the main table to get a list of upcoming classes, and the other two is to get the course title and the instructor's name, the problem is if an instructor has not been assgned the result does not get returned it just skips that row, I'm guessing it's becuase the instructor's id is 0 not sure?  If the courses.id and users.id is set it will get the row, but if ether of them are set to 0 then it skips the row.  Can you take a look at my query and see where I'm going wrong, thanks everyone.

 

SELECT
classes.id,
classes.state,
classes.date AS created,
classes.city,
users.`name` AS instructor,
courses.title AS course
FROM
classes
INNER JOIN users ON users.id = classes.instructor_id
INNER JOIN courses ON courses.id = classes.courseid

 

Link to comment
Share on other sites

Hi

 

If you want a row returned when one of the joined tables doesn't have a matching row then use an outer join

 

SELECT
classes.id,
classes.state,
classes.date AS created,
classes.city,
users.`name` AS instructor,
courses.title AS course
FROM
classes
INNER JOIN users ON users.id = classes.instructor_id
LEFT OUTER JOIN courses ON courses.id = classes.courseid

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.