152

I have the following query:

SELECT
  dashboard_data.headline,
  dashboard_data.message,
  dashboard_messages.image_id 
FROM dashboard_data
INNER JOIN dashboard_messages
  ON dashboard_message_id = dashboard_messages.id

So I am using an INNER JOIN and grabbing the image_id. So now, I want to take that image_id and turn it into images.filename from the images table.

How can I add that in to my query?

1

4 Answers 4

263

You can simply add another join like this:

SELECT dashboard_data.headline, dashboard_data.message, dashboard_messages.image_id, images.filename
FROM dashboard_data 
    INNER JOIN dashboard_messages 
        ON dashboard_message_id = dashboard_messages.id
    INNER JOIN images
        ON dashboard_messages.image_id = images.image_id 

However be aware that, because it is an INNER JOIN, if you have a message without an image, the entire row will be skipped. If this is a possibility, you may want to do a LEFT OUTER JOIN which will return all your dashboard messages and an image_filename only if one exists (otherwise you'll get a null)

SELECT dashboard_data.headline, dashboard_data.message, dashboard_messages.image_id, images.filename
FROM dashboard_data 
    INNER JOIN dashboard_messages 
        ON dashboard_message_id = dashboard_messages.id
    LEFT OUTER JOIN images
        ON dashboard_messages.image_id = images.image_id 
Sign up to request clarification or add additional context in comments.

4 Comments

anybody know how this actually works? Is the second join joining the results of the first join with table 3, or is it joining table 1 with table 3 separately? (i.e., joining table 1 with table 2, and then joining table 1 separately with table 3, then returning it all back?) Does that make sense? I ask because I've been doing multi-table joins like this, and sometimes get unexpected results.
That's what the ON clause will tell you. The ON clause for the 2nd join (joining the third table) is joining dashboard_messages to images on the image_id field in each table. So, in this case it's A to B then B to C. It's under your control. You could make it A to B and A to C if you need
This query works all the time! For me, 2 yrs ago it worked on another MySQL Database, this time it worked on another Database. Thanks.
@AaronWallentine Can you give an example of unexpected results? Or any example when the order matters? Your question makes sense, but I think join operations always commute: A~B~C is the same as (A~B)~C, which is the same as A~(B~C). This is because any join A~B is a Cartesian product of rows filtered by an appropriate condition. A~B~C is a Cartesian product of three relations, filtered by the conjunction of the two condition. The last statement is intuitively clear, but must be proved by showing that filtering for A~B can be postponed until after the second Cartesian product.
17

Just add another join:

SELECT dashboard_data.headline,
       dashboard_data.message,
       dashboard_messages.image_id,
       images.filename 
FROM dashboard_data 
    INNER JOIN dashboard_messages
            ON dashboard_message_id = dashboard_messages.id 
    INNER JOIN images
            ON dashboard_messages.image_id = images.image_id

Comments

9

I shared my experience of using two LEFT JOINS in a single SQL query.

I have 3 tables:

Table 1) Patient consists columns PatientID, PatientName

Table 2) Appointment consists columns AppointmentID, AppointmentDateTime, PatientID, DoctorID

Table 3) Doctor consists columns DoctorID, DoctorName


Query:

SELECT Patient.patientname, AppointmentDateTime, Doctor.doctorname

FROM Appointment 

LEFT JOIN Doctor ON Appointment.doctorid = Doctor.doctorId  //have doctorId column common

LEFT JOIN Patient ON Appointment.PatientId = Patient.PatientId      //have patientid column common

WHERE Doctor.Doctorname LIKE 'varun%' // setting doctor name by using LIKE

AND Appointment.AppointmentDateTime BETWEEN '1/16/2001' AND '9/9/2014' //comparison b/w dates 

ORDER BY AppointmentDateTime ASC;  // getting data as ascending order

I wrote the solution to get date format like "mm/dd/yy" (under my name "VARUN TEJ REDDY")

Comments

6

Multi joins in SQL work by progressively creating derived tables one after the other. See this link explaining the process:

http://web.archive.org/web/20240302193618/https://www.interfacett.com/blogs/multiple-joins-work-just-like-single-joins/

2 Comments

Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.
And the link is no longer available... I've changed it for an archive link instead

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.