我有2个表:用户和日志。目前我的查询看起来像这样。
$stmt = $this->db->prepare("SELECT u.id, u.email, u.salt, u.pass, u.approved, u.ban, u2.status FROM `users` AS u LEFT OUTER JOIN `log` AS u2 ON u2.user_id = u.id WHERE u.email = ?") or die($this->db->error);
$stmt->bind_param("s", $_POST['email']) or die($stmt->error);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows == 0) {
die($this->ajax->respond(7));
}
$data = array();
$stmt->bind_result($data['id'], $data['email'], $data['salt'], $data['pass'], $data['approved'], $data['ban'], $data['status']) or die($stmt->error);
$stmt->fetch() or die($stmt->error);
$stmt->close() or die($stmt->error);
日志表的状态列 - 表示用户是否已登录。我想要做的是检查users
表中是否存在电子邮件,并计算log
表中status=1
的行数。这是唯一的查询吗?
换句话说:
这是日志表
查看status = 1的行。这表示用户1当前已登录。为了防止来自不同浏览器的第二次登录我想首先检查用户表中的电子邮件(基本登录程序)并在那一刻检查如果用户未登录(通过计算日志表中status = 1的行)
答案 0 :(得分:1)
只需将查询更改为此 -
SELECT u.id, u.email, u.salt, u.pass, u.approved,
u.ban, SUM(u2.status) AS status FROM `users` AS u
LEFT JOIN `log` AS u2 ON u2.user_id = u.id
WHERE u.email = ?
GROUP BY u.id
还有很多其他方法可以做到这一点。