Sandor
Sandor

Reputation: 41

query SQL select from many tables by id?

this is my sql query:

    function show($conn){


        $ask="  SELECT  people.per_id, people.per_name , people.hos_id,

        people.car_id, people.us_id FROM people

        inner join house  on house.hos_id = house.hos_id

        inner join car  on people.car_id =  car.car_id

        inner join user  on people.us_id = user.us_id  ";

    $query = $conn->prepare($ask);

    $query->execute();


        return $query;
    }

example of what the query shows

 people.per_id = 1
 people.per_name = mark
 people.hos_id = 3
 people.car_id = 5
 people.us_id = 7

what I want to see

people.per_id = 1
people.per_name = mark
people.hos_id = green house
people.car_id = suv 
people.us_id = faster

How can I access by id to another data from the tables house, car and user?

Upvotes: 1

Views: 73

Answers (2)

Bhargav Chudasama
Bhargav Chudasama

Reputation: 7161

try this

use house.house_name instead of people.hos_id, car.car_name, user.user_name and write people.hos_id = house.hos_id instead of house.hos_id = house.hos_id

function show($conn){

    $ask="  SELECT  people.per_id, people.per_name , house.house_name,
    car.car_name, user.user_name FROM people
    inner join house  on people.hos_id = house.hos_id
    inner join car  on people.car_id =  car.car_id
    inner join user  on people.us_id = user.us_id  ";

    $query = $conn->prepare($ask);
    $query->execute();
    return $query;
}

Upvotes: 1

Fahmi
Fahmi

Reputation: 37473

You can try below - You need to selct name from other tables like house.name,car.name, user.name instead of ids columns

SELECT  people.per_id, people.per_name , house.name,car.name, user.name FROM people
inner join house  on people.hos_id = house.hos_id
inner join car  on people.car_id =  car.car_id
inner join user  on people.us_id = user.us_id

Upvotes: 0

Related Questions