我有下表
uId | groupId
==============
1 12
3 66
如何编写mySQL语句来检查用户是否存在于组中?
我的尝试:
SELECT FROM usertable(uId) WHERE usertable.uId = $uId
上述查询仅检查用户是否存在,但不检查用户是否属于特定组。
答案 0 :(得分:2)
只需将该条件附加到where
子句中,例如:
SELECT uId
FROM usertable
WHERE usertable.uId = 1
AND groupId = 12
答案 1 :(得分:0)
只需将groupId
参数添加到查询WHERE
子句中。
$userId = x; // some user id
$groupId = y; // some group id
$con; // your mysqli instance
if (!$stmt = $con->prepare('SELECT 1 FROM usertable WHERE uId = ? AND groupId = ?')) {
throw new Exception($con->error, $con->errno);
}
$stmt->bind_param('ii', $userId, $groupId);
if (!$stmt->execute()) {
throw new Exception($stmt->error, $stmt->errno);
}
if ($stmt->fetch()) {
// user exists and is in group
}
答案 2 :(得分:0)
$query = mysql_query("Select from usertable(uId) where usertable.uId = $uId and groupId=$gid");
if(mysql_num_rows($query)!=0){
echo "user exists!";
}else
{
echo "user not exists!";
}