Search code examples
sql-servermany-to-manyinformation-retrieval

Retrieve Data from many-to-many table in SQL Server


I'm new to SQL Server. I don't know how I can retrieve data from a many-to-many table.

My tables looks like:

Hospitals:

enter image description here

Treatments:

enter image description here

hospitals_treatments:

enter image description here

How can I view my data like on table hospital_treatment but instead of id I want name from respective tables. How can I do that?


Solution

  • Try this

    SELECT
    b.name, c.name
    FROM
        Hospital_treatment a
    JOIN Hospitals b
        ON a.hospitalshospital_id = b.id
    JOIN Treatments c
        ON a.treatmentstreament_id = c.Id