Mysql – theSQL query with field replacemnt

MySQL

I'm need to run a query from mySQL 5.6 which involves looking up date from different tables. I'm new to mySQL and I'm not sure what this operation is called, or else I would have just googled the subject… I'm hoping to get someone to point me in the right direction… Please note, I can't change the structure of the tables, as this is pre-populated from the application I am using.

example:

3 tables in the same local database: network_segments, Buildings, Departments

network_segments description:

network_segment_ID, display_name, starting_ip, ending_ip, building, department
1, 10.0.0.1, 10.0.0.254, 1, 2
2, 10.1.0.1, 10.1.0.254, 2, 3
3, 10.4.0.1, 10.4.0.254, 3, 1

building table description:

building_ID, description
1, ACHQ
2, EHQ
3, AHQ

departments table description:

department_ID, description
1, marketing
2, sales
3, hr

I want to "select * from network_segments" but in stead of the building and department IDs coming back, I'd like the description field for those IDs:

1, 10.0.0.1, 10.0.0.254, ACHQ, sales
2, 10.1.0.1, 10.1.0.254, EHQ, hr
3, 10.4.0.1, 10.4.0.254, AHQ, marketing

any advise would be appreciated…

Thank you!

Best Answer

select network_segment_ID, display_name, starting_ip, ending_ip, bld.description, dept.description
from network_segments
join buildings bld on (building = building_ID)
join departments dept on (department = department_ID);