Rabu, 19 Oktober 2016

SQL join 4 Tables

Once again, a posting that will help me recalling how to join more than 2 tables in sql. In this case, I had to join 4 tables to fetch the data that I needed.

Table 1 : member

id_memberid_cityid_majorid_facultynamephone
mbr0001111Rafi Hajid0812234586
mbr0002122Almira08775123495

Table 2:City

id_cityname
1Surabaya
2Kediri

Table 3: Major

id_majorname
1S1 SI
2D3 SI

Table 4: Faculty

id_facultyname
1Science and Technology
2Vocational

The Information that I needed from those tables were name of the member, phone's Nr., City's name, Major's name and Faculty's name.

This is the SQL to retrieve those information:

select m.name, m.phone, c.name as city, mj.name as major, f.name as faculty
from member as m 
join city as c on m.id_city = c.id_city
join major as mj on m.id_major = mj.id_major
join faculty as f on m.id_faculty = f.id_faculty

The result of the query would be:

namephonecitymajorfaculty
Rafi Hajid0812234586SurabayaS1 SIScience and Technology
Almira08775123495SurabayaD3 SIVocational


Hope this can help you, somehow....

Tidak ada komentar:

Posting Komentar