www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] MySQLi join based on crosstable

  1. #1
    Join Date
    May 2006
    Posts
    140

    resolved [RESOLVED] MySQLi join based on crosstable

    I've been searching and trying this for a while but I can't seem to find out how to do this. I have the following 3 tables as an example:

    owners
    id
    name


    pets
    id
    name

    petsOwned
    ownerID
    petID


    I'm looking for a single MySQLi query to get both the ownername and petname of one specific pet.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,528
    Code:
    select owners.name as owner_name, pets.name as pet_name
    from pets
    inner join petsOwned on petsOwned.petId = pets.id
    inner join owners on petsOwned.ownerId = owners.id
    where pets.id = ?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2006
    Posts
    140
    Quote Originally Posted by NogDog View Post
    Code:
    select owners.name as owner_name, pets.name as pet_name
    from pets
    inner join petsOwned on petsOwned.petId = pets.id
    inner join owners on petsOwned.ownerId = owners.id
    where pets.id = ?
    Thank you! That works perfectly

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles