mysql加入两个表格,给表1的所有条目
我有三张桌子。
表1:TBL_USER
ID | 用户名 |
---|---|
1 | 用户A |
2 | 用户B |
表2:TBL_Location
ID | 位置 |
---|---|
1 | 2城市 |
2 | 城市2 |
3 | 城市3 |
表3:tbl_userlocation
fk_user fl_user | fl_location |
---|---|
1 | 1 |
2 | 1 2 |
2 | 2 |
现在我有一个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
id | username |
---|---|
1 | User A |
2 | User B |
Table 2: tbl_location
id | location |
---|---|
1 | City 1 |
2 | City 2 |
3 | City 3 |
Table 3: tbl_userlocation
fk_user | fl_location |
---|---|
1 | 1 |
2 | 1 |
2 | 2 |
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
子句在 之后应用
JOIN
。为了满足您的需求,您需要在 或 之前过滤 。将用户过滤器包括在JOIN谓词中,或在过滤用户的子问题上加入(前者是通常的,清洁的方法)。
或者...
The
WHERE
clause is applied after theJOIN
.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).
Or...