【问题标题】:Retrieve info from table with 2 conditions and 3 table从具有 2 个条件和 3 个表的表中检索信息
【发布时间】:2020-04-05 11:41:25
【问题描述】:
<?php

define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'websitefinal');

//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    die();
} else {
}
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $id = $_POST['user_id'] ?? '';

    $query = "SELECT UL.* FROM users UL INNER JOIN active_crew L JOIN search SL      ON L.user_id = UL.user_id AND L.search_id = SL.search_id";
    $sql = mysqli_query($conn, $query);
    $result2 = array();
    while ($row = mysqli_fetch_array($sql)) {
        array_push($result2, array(
            'user_id' => $row[0],
            'name_full' => $row[1],
            'Propic' => $row[4]));
    }

    echo json_encode($result2);
    mysqli_close($conn);
}

我正在尝试在活动工作人员表中检索在特定搜索“搜索表”上值班的用户“用户表”的信息,但我没有收到任何信息

我想从“user_id”主键和外键中检索数据的用户表

crew 表只有 2 列 search_id 和 user_id 都是外键

应具有职责描述“search_id”主外键的搜索表

key 关系对 search_id.crew 和 search_id.search 的所有列都有效 user_id.crew 和 user_id.user

【问题讨论】:

  • 帮助我们帮助您 - 分享一些示例数据以及您希望为此示例获得的结果

标签: php mysql join


【解决方案1】:

您必须在每个JOINON 子句中设置每个连接的条件,而不是在最后一个ON 中设置所有条件:

SELECT UL.* 
FROM users UL 
INNER JOIN active_crew L ON L.user_id = UL.user_id 
INNER JOIN search SL ON L.search_id = SL.search_id

【讨论】:

    猜你喜欢
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多