简体   繁体   中英

MySQL: Joining a joint table

I have three tables

Question ID | question_name

This table lists a number of questions, each question has there id.

Location ID | location_name

This question lists a number of locations, each location has its ID

Question_Location ID | question_id | location_ID

This question matches the question to location on the basis of the key.

How would I create a table that provides me with: question_name | location_name

Where each question is matched to its location but instead of seeing the IDs I see the question_name and location_name.

I could join the location and the question_location table and the question_location table and the location table but I don't manage to put it together so that I have the output as described.

Select q.Question_Name, l.location_name
from Question q
Inner join Question_Location ql on ql.Question_id = q.question_id
inner join Location l on l.location_id = ql.location_id

Assuming that every question has a location and not null, if that's the case you will be better with a left join on the location join.

You can try it:

SELECT q.question_name, l.location_name
FROM Question_Location ql
LEFT JOIN Question q on ql.question_id = q.ID
LEFT JOIN Location l ON ql.location_ID = l.ID
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM