我有一张经销商和用户的表格。现在每个经销商都有自己的用户。每个用户都通过一个名为key的字段映射到该经销商。因此,每个用户都有一个名为key的字段,其中将保存代理商的密钥。
现在我有一个叫做分配块的文件。这是经销商可以拥有的最大用户数。现在,例如,经销商拥有100个用户的分配块,但最初只插入10个用户。所以我想打印他允许添加的剩余用户。所以分配block-intitals_users。这将是100-10所以在经销商登录时,我想显示他可以添加多少用户。
请帮我生成一个查询来计算他可以利用的其余用户。
答案 0 :(得分:1)
让我们分步说明:让我们说我们在您的表格中使用经销商Zalak
,其分配大小为500
,并且密钥为Zalak-+76-6-500-1
和6
SELECT COUNT(*) AS CurrentUserTotal FROM Users WHERE key = 'Zalak-+76-6-500-1';
SELECT allocation_block AS AllowedUserTotal FROM Resellers WHERE id '6';
SELECT
COUNT(u.*) AS CurrentUserTotal,
r.allocation_block AS AllowedUserTotal
FROM Users u
JOIN Resellers r
ON (u.key = r.key)
WHERE r.id = '6';
SELECT
COUNT(u.*) AS CurrentUserTotal,
r.allocation_block AS AllowedUserTotal,
SUM(AllowedUserTotal - CurrentUserTotal) AS RemainingUserTotal
FROM Users u
JOIN Resellers r
ON (u.key = r.key)
WHERE r.id = '6';
使用CI,您现在可以执行此操作:
public function getRemainingAllowedUsers( $reseller_id ) {
$SQL = "
SELECT
COUNT(u.*) AS CurrentUserTotal,
r.allocation_block AS AllowedUserTotal,
SUM(AllowedUserTotal - CurrentUserTotal) AS RemainingUserTotal
FROM Users u
JOIN Resellers r
ON (u.key = r.key)
WHERE r.id = '" . $reseller_id . "';
";
$query = $this->db->query($SQL);
$result = $query->row();
$current_users = $result->CurrentUserTotal;
$allowed_users = $result->AllowedUserTotal;
$remaining_users = $result->RemainingUserTotal;
}