Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Help need ed to write SQL query

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    It's called a JOIN. You really should read up on SQL enough to understand at least simple JOINs.

    Code:
    SELECT floorid, floors.name, buildings.name
    FROM floors, buildings
    WHERE floors.buildingID = buildings.buildingID
    ORDER BY floorid
    That can also be written as:
    Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •