...

View Full Version : Help need ed to write SQL query



coolguyraj
03-10-2009, 10:35 AM
I have two tables that are referenced:

1.Buildings

fields:buildingid,name

2.floors
fields:floorId,name,BuildingId(linked to buildings table using buildingId)

Curently if i select all the details from floors table it will give all the details.

But i want Building name to be displayed instead of buildingId.

Output should look like:

floorid name buildingname

1 first xyz building
2 second xyz building
3 third xyz building


what is the SQL query required to get the above output.

Old Pedant
03-10-2009, 10:47 AM
It's called a JOIN. You really should read up on SQL enough to understand at least simple JOINs.



SELECT floorid, floors.name, buildings.name
FROM floors, buildings
WHERE floors.buildingID = buildings.buildingID
ORDER BY floorid

That can also be written as:


SELECT floorid, floors.name, buildings.name
FROM floors INNER JOIN buildings
ON floors.buildingID = buildings.buildingID
ORDER BY floorid


Please tell me this is not homework.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum