I am fairly new to MySQL and I am having some trouble figuring this out.
I have two tables called doctors and doctor_tags
Fields for doctors: doctor_id, doctor_name
Fields for doctor_tags: id, doctor_id, tag_name
Now for a specific doctor_id I need to find related doctors. Doctors are related if they have the same tag_name. Once I find this relation I need to get doctor_name of all these doctors back.
I am almost completely lost I've got this far (I am sure it's wrong)
SELECT doctors.doctor_name, doctors.doctor_id FROM doctors INNER JOIN doctors_tags ON doctors.doctor_id = doctors_tags.doctor_id ...
I konw this query is pretty useless but I know we need an some type of join.
For whoever is kind enough to comeup with some sort of query, I would be very thankful if you could explain each part of it in the process. Thanks :)
EDIT: Part 2
If lets say we introduce another table that has an N:N relationship between doctor_id and tag_id
Table fields
doctor_tags_joins: doctor_id,tag_id
and so the fields for the tag table change to
doctors_tags: tag_id, tag_name and so on...
how can we do the same thing with this additional table included into the mix.