Home >Database >Mysql Tutorial >How to Create an SQL View Joining Three Tables (StudentSignUp, Incoming_Applications_Current, and Halls)?
In the field of database management, it is often necessary to merge data from multiple tables. A common scenario involves joining three or more tables to gain comprehensive insights. In this example, the goal is to create a view that seamlessly combines information from three tables: StudentSignUp, Incoming_Applications_Current, and Halls.
The initial query provided focuses on joining the StudentSignUp and Incoming_Applications_Current tables, successfully retrieving student information along with their dorm preferences. However, residence hall preferences are represented by ID numbers rather than corresponding names. To complete the view, a third table, Halls, needs to be merged.
A modified query can be used to achieve the desired result:
<code class="language-sql">SELECT s.StudentID, s.FName, s.LName, s.Gender, s.BirthDate, s.Email, r.HallPref1, h1.HallName AS HallPref1Name, r.HallPref2, h2.HallName AS HallPref2Name, r.HallPref3, h3.HallName AS HallPref3Name FROM dbo.StudentSignUp AS s INNER JOIN RoomSignUp.dbo.Incoming_Applications_Current AS r ON s.StudentID = r.StudentID INNER JOIN HallData.dbo.Halls AS h1 ON r.HallPref1 = h1.HallID INNER JOIN HallData.dbo.Halls AS h2 ON r.HallPref2 = h2.HallID INNER JOIN HallData.dbo.Halls AS h3 ON r.HallPref3 = h3.HallID;</code>
This modified query introduces three additional JOINs between the Incoming_Applications_Current table and the Halls table. Each JOIN is executed using the HallPref ID as the matching condition. Therefore, the query retrieves all necessary information from three tables, including the student's dorm name preference, not just the dorm ID.
By implementing this comprehensive query, you can effectively create a view that presents a complete picture of student information, including their dorm preferences represented by human-readable names.
The above is the detailed content of How to Create an SQL View Joining Three Tables (StudentSignUp, Incoming_Applications_Current, and Halls)?. For more information, please follow other related articles on the PHP Chinese website!