如何在单个SQL查询中连接两个表?

时间:2015-12-31 13:05:08

标签: php mysql database

我在一个数据库中有三个表.. (1)user_details_home (2)user_details_car (3)用户  我想在一个查询中从两个表中选择数据。以下是我想如何在单个{{1}中从这两个表(user_details_homeuser_details_car)中选择数据的片段}

$query

$query = "SELECT `users`.`email`, `users`.`username`,`users`.`last_login`, `user_details_home`.*
          FROM `users`, `user_details_home`
          WHERE `users`.`user_id` = :id
          AND `users`.`user_id` = `user_details_home`.`user_id`";

所以最终我可以执行此查询

$query = "SELECT `users`.`email`, `users`.`username`,`users`.`last_login`, `user_details_car`.*
          FROM `users`, `user_details_car`
          WHERE `users`.`user_id` = :id
          AND `users`.`user_id` = `user_details_car`.`user_id`";

先谢谢!

这是表格结构 user_details_home user_details_car

图片只是一个演示结构

以下是我的问题的完整路径

//(1)

$result = $this->db->select($query, array( 'id' => $this->userId ));

(2)此代码在AsAjax.php中返回为:(Ajax)

public function getAll() {
    $query = "SELECT `as_users`.`email`, `as_users`.`username`,`as_users`.`last_login`, `as_user_details`.*
                FROM `as_users`, `as_user_details`
                WHERE `as_users`.`user_id` = :id
                AND `as_users`.`user_id` = `as_user_details`.`user_id`";

    $result = $this->db->select($query, array( 'id' => $this->userId ));

    if ( count ( $result ) > 0 )
        return $result[0];
    else
        return null;
}

(3)然后这个案例作为另一个user.js文件中的动作:(以下是Javascript文件)

switch ($action) {
case "getUserDetails":
    onlyAdmin();

    $user = new ASUser($_POST['userId']);
    echo json_encode( $user->getAll() );
    break;
}

//显示模态    modal.modal('显示&#39);

//设置用户名(模态窗口标题)加载...    username.text($ _ lang.loading);

//显示ajax加载gif    ajaxLoading.show();

//隐藏详细信息正文    detailsBody.hide();

$。AJAX({        url:" ASEngine / ASAjax.php",        键入:" POST",        数据:{            动作:" getUserDetails",            userId:userId        },        成功:功能(结果){            //将结果解析为JSON            var res = JSON.parse(result);

users.displayInfo = function (userId) {
var username    = $("#modal-username"),
    email       = $("#modal-email"),
    firstName   = $("#modal-first-name"),
    lastName    = $("#modal-last-name"),
    address     = $("#modal-address"),
    age         = $("#modal-age"),
    phone       = $("#modal-phone"),
    last_login  = $("#modal-last-login"),
    ajaxLoading = $("#ajax-loading"),
    detailsBody = $("#details-body"),
    modal       = $("#modal-user-details");

});

};

(4)最后按下按钮信息:

       //update modal fields
       username .text(res.username);
       email    .text(res.email);
       firstName.text(res.first_name);
       lastName .text(res.last_name);
       address  .text(res.address);
       age      .text(res.age);
       phone    .text(res.phone);
       last_login.text(res.last_login);

       //hide ajax loading
       ajaxLoading.hide();

       //display user info
       detailsBody.show();
   }

基本上,函数getAll()在第二步链接到getUserDetails,然后getUserDetails在第三步链接到user.displayInfo,而user.displayInfo在第四步链接。所以我只需要执行两个$ query作为一个。           我希望这会有所帮助!

3 个答案:

答案 0 :(得分:1)

我认为你正在寻找这个:

$query = "
SELECT user_details_car.first_name,user_details_car.phone,user_details_car.address,
user_details_home.first_name,user_details_home.phone,user_details_home.address
FROM user_details_car, user_details_home
WHERE user_details_car.user_id = user_details_home.user_id";

答案 1 :(得分:0)

假设user_details_homeuser_details_car具有相同的结构(相同的列名,相同的列顺序,相同的数据类型),您可以使用:

SELECT users.email, users.username, users.last_login, user_details_home.*
FROM users, user_details_home
WHERE users.user_id = :id
AND users.user_id = user_details_home.user_id
UNION ALL
SELECT users.email, users.username,users.last_login, user_details_car.*
FROM users, user_details_car
WHERE users.user_id = :id
AND users.user_id = user_details_car.user_id

这不会消除重复。如果您需要更好的答案,请详细说明(数据结构和要求)。

答案 2 :(得分:0)

两者的结合应如下所示:

SELECT `users`.`email`, `users`.`username`,`users`.`last_login`, `user_details_home`.*, `user_details_car`.*
          FROM `users`, `user_details_home`,`user_details_car`
          WHERE `users`.`user_id` = :id
          AND `users`.`user_id` = `user_details_home`.`user_id`
          AND `users`.`user_id` = `user_details_car`.`user_id`;

警告:如果用户有车一个家,这只会返回行。

使用left join改善方法:

SELECT `users`.`email`, `users`.`username`,`users`.`last_login`, `user_details_home`.*, `user_details_car`.*
          FROM `users`
          LEFT JOIN `user_details_home` ON `users`.`user_id` = `user_details_home`.`user_id`
          LEFT JOIN `user_details_car` ON AND `users`.`user_id` = `user_details_car`.`user_id`
          WHERE `users`.`user_id` = :id;

可能*语法可能不正确,你必须明确列出列(无论哪种方式对性能更好),我没有测试过。