mysql加入两个表格,给表1的所有条目

发布于 2025-01-21 08:10:34 字数 2205 浏览 0 评论 0原文

我有三张桌子。

表1:TBL_USER

ID用户名
1用户A
2用户B

表2:TBL_Location

ID位置
12城市
2城市2
3城市3

表3:tbl_userlocation

fk_user fl_userfl_location
11
21 2
22

现在我有一个html页面来编辑用户数据

我有两个查询。一种用于阅读用户数据,一个用于该位置。

public function readOneUser($id)
{
    $stmt = $this->pdo->prepare("SELECT * FROM tbl_user WHERE id = :id");
    $stmt->execute([
        'id' => $id
    ]);
    $stmt->setFetchMode(PDO::FETCH_CLASS, "administration\\CMR\\UserModel");
    $res = $stmt->fetch(PDO::FETCH_CLASS);
    return $res;
}

没关系。一切都很好。我有一个普通的用户数据,例如用户名,邮件,电话...

现在我想要所有位置的完整列表,但是(这就是问题)应检查所有分配用户的位置。不得检查未分配用户的位置,而应列出。

<?php
    foreach ($readLocations AS $location):
    ?>
    <input type="checkbox" name="location" <?= htmlspecialchars($location->id == $location->fk_location) ? "checked" : "" ?> value="<?= $location->id; ?>"><span><?= $location->location; ?></span><br>
    <?php
    endforeach;
?>

我有这个查询。我知道这是错误的,但这就是我的“最好”。希望您能帮助我正确。

public function readAllLocationsForEdit($id)
{
    $stmt = $this->pdo->prepare("
        SELECT tbl_location.*, tbl_userlocation.*
        FROM tbl_location
        LEFT JOIN tbl_userlocation ON tbl_userlocation.fk_location = tbl_location.id 
        WHERE fk_user = :id
        GROUP BY location");
    $stmt->execute([
        'id' => $id
    ]);
    $res = $stmt->fetchAll(PDO::FETCH_CLASS, "administration\\CMR\\LocationModel");
    return $res;
}

通过此,我得到了分配用户的所有位置,而不是未分配用户的位置。我知道,何处子句是不正确的,但是我测试过的所有其他条件都不会给我我想要的结果。

I have three tables.

Table 1: tbl_user

idusername
1User A
2User B

Table 2: tbl_location

idlocation
1City 1
2City 2
3City 3

Table 3: tbl_userlocation

fk_userfl_location
11
21
22

Now I have a HTML-Page to edit user data

I have two querys. One for reading the userdata and one for the locations.

public function readOneUser($id)
{
    $stmt = $this->pdo->prepare("SELECT * FROM tbl_user WHERE id = :id");
    $stmt->execute([
        'id' => $id
    ]);
    $stmt->setFetchMode(PDO::FETCH_CLASS, "administration\\CMR\\UserModel");
    $res = $stmt->fetch(PDO::FETCH_CLASS);
    return $res;
}

That's fine. All is good. I have the normal userdata like username, mail, phone...

Now I want a complete list of all locations, BUT (and that's the problem) all locations where the user is assigned should be checked. Locations where the user is not assigned should not be checked but listed.

<?php
    foreach ($readLocations AS $location):
    ?>
    <input type="checkbox" name="location" <?= htmlspecialchars($location->id == $location->fk_location) ? "checked" : "" ?> value="<?= $location->id; ?>"><span><?= $location->location; ?></span><br>
    <?php
    endforeach;
?>

I have this query. I know it's wrong, but that's the "best" I have. Hope you can help me to make it correct.

public function readAllLocationsForEdit($id)
{
    $stmt = $this->pdo->prepare("
        SELECT tbl_location.*, tbl_userlocation.*
        FROM tbl_location
        LEFT JOIN tbl_userlocation ON tbl_userlocation.fk_location = tbl_location.id 
        WHERE fk_user = :id
        GROUP BY location");
    $stmt->execute([
        'id' => $id
    ]);
    $res = $stmt->fetchAll(PDO::FETCH_CLASS, "administration\\CMR\\LocationModel");
    return $res;
}

With this I get all locations where a user is assigned, but not the locations where the user is not assigned. I know, that the WHERE-clause is not correct, but all other I tested give me not the result I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2025-01-28 08:10:35

子句在 之后应用 JOIN

为了满足您的需求,您需要在 或 之前过滤 。将用户过滤器包括在JOIN谓词中,或在过滤用户的子问题上加入(前者是通常的,清洁的方法)

     FROM tbl_location
LEFT JOIN tbl_userlocation
       ON tbl_userlocation.fk_location = tbl_location.id 
      AND tbl_userlocation.fk_user     = :id

或者...

     FROM tbl_location
LEFT JOIN (SELECT * FROM tbl_userlocation WHERE fk_user = :id) AS tbl_userlocation 
       ON tbl_userlocation.fk_location = tbl_location.id

The WHERE clause is applied after the JOIN.

For your needs, you need to filter the userlocation map either before or during the join. Either include the user filter in the join predicate, or join on a sub-query that filters the user (the former being the usual, and cleaner, approach).

     FROM tbl_location
LEFT JOIN tbl_userlocation
       ON tbl_userlocation.fk_location = tbl_location.id 
      AND tbl_userlocation.fk_user     = :id

Or...

     FROM tbl_location
LEFT JOIN (SELECT * FROM tbl_userlocation WHERE fk_user = :id) AS tbl_userlocation 
       ON tbl_userlocation.fk_location = tbl_location.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文