首先是我的桌子:
+----------+ +--------------+
| users | | users_groups | +--------------+
+----------+ +--------------+ | groups |
| user_id |----->| user_id | +--------------+
| username | | group_id |<----| group_id |
| realname | +--------------+ | group_name |
| password | | group_desc |
+----------+ +--------------+
从其他PHP脚本我有一个变量$group_id
,现在我想要这个组中的所有用户名和user_ids($group_id
)。
我尝试了这个,但它没有工作:
SELECT u.username, u.user_id
FROM users u
WHERE users_groups.group_id = $group_id
查询应该如何?加入的东西,不是吗?
我的PHP代码:
$group_id = $_POST["group_id"];
$query_user = mysqli_query($db,
"SELECT
User.user_id, User.username
FROM users User
INNER JOIN user_groups UserGroup
ON User.user_id = UserGroup.user_id
WHERE UserGroup.group_id = $group_id
");
while ($row_user = mysqli_fetch_object($query_user)) {
echo'
'.$row_user->username.'<br>
';
}
答案 0 :(得分:1)
21121
12121
11221
11122
11111
希望这样可以正常工作!
修改强>
SELECT
User.user_id, User.username
FROM users User
INNER JOIN users_groups UserGroup
ON User.user_id = UserGroup.user_id
WHERE UserGroup.group_id = $group_id
答案 1 :(得分:0)
这将有效:
SELECT u.username, u.user_id
FROM users u
WHERE users_groups.group_id = $group_id && users_groups.user_id = u.user_id