查询SQL从多个表中选择ID?

时间:2018-11-26 04:55:31

标签: mysql sql

这是我的sql查询:

    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;
    }

查询显示示例

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

我想看的

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

如何通过ID来访问桌房,汽车和用户中的其他数据?

2 个答案:

答案 0 :(得分:1)

尝试

使用house.house_name代替people.hos_idcar.car_name, user.user_name并写people.hos_id = house.hos_id代替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;
}

答案 1 :(得分:0)

您可以在下面尝试- 您需要从其他表中选择名称,例如house.name,car.name, user.name 而不是ids

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