2017-05-09 46 views
2

我有一个系统,用户可以为不同类型的贡献获得一个或多个积分。这些存储2下表中:如何从相乘结果中选择随机唯一值

CREATE TABLE user_contribution_types (
    type_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, 
    title VARCHAR(255) NOT NULL, 
    credits DECIMAL(5,2) UNSIGNED NOT NULL, 
    valid TINYINT(1) UNSIGNED NOT NULL DEFAULT 1, 

    PRIMARY KEY (type_id) 
); 

CREATE TABLE user_contributions (
    user_id INTEGER UNSIGNED NOT NULL, 
    type_id INTEGER UNSIGNED NOT NULL, 
    create_date DATETIME NOT NULL, 
    valid TINYINT(1) UNSIGNED NOT NULL DEFAULT 1, 

    FOREIGN KEY (user_id) 
    REFERENCES users(user_id), 
    FOREIGN KEY (type_id) 
    REFERENCES user_contribution_types(type_id) 
); 

我可以选择,因为一个特定的日期与以下赚取的总学分:

SELECT SUM(credits) AS total 
FROM user_contribution_types AS a 
JOIN user_contributions AS b ON a.type_id = b.type_id 
WHERE b.create_date >= '2017-05-01 00:00:00' 
     AND a.valid = TRUE 
     AND b.valid = TRUE 

同样,我也可以包括匹配b.user_id找到总学分为那个特定的用户。

我想要做的就是将每次获得的信用作为赠品进行处理,并从总数中选择3个随机(唯一)user_id秒。所以如果一个用户获得了26个学分,他们将有26个获胜机会。

这怎么可以用SQL来完成呢,还是在应用程序级别做更有意义呢?我宁愿选择尽可能接近真正随机的解决方案。

回答

0

嗯,我无法让戈登的代码无误地运行,所以我最终恢复为应用程序逻辑,并遵循解决方案found here。例如:除非一个替代的解决方案张贴,我会接受这个作为答案

$ts = '2017-01-01 00:00:00'; 
$first_place = getWinner($ts); 
$second_place = getWinner($ts, [$first_place]); 
$third_place = getWinner($ts, [$first_place, $second_place]); 

// pick a random winner since a given date 
// optionally exclude certain users 
public function getWinner($date, array $exclude = []) { 
    if (!empty($exclude)) { 
     $in = implode(',', array_fill(0, count($exclude), '?')); 
     array_unshift($exclude, $date); 

     $sql = "SELECT b.user_id, SUM(credits) AS total 
       FROM  user_contribution_types AS a 
       JOIN  user_contributions AS b ON a.type_id = b.type_id 
       WHERE b.create_date >= ? 
         AND b.user_id NOT IN ($in) 
         AND a.valid = TRUE 
         AND b.valid = TRUE 
       GROUP BY b.user_id"; 
     $sth = $this->db->prepare($sql); 
     $sth->execute($exclude); 
    } else { 
     $sql = "SELECT b.user_id, SUM(credits) AS total 
       FROM  user_contribution_types AS a 
       JOIN  user_contributions AS b ON a.type_id = b.type_id 
       WHERE b.create_date >= :date 
         AND a.valid = TRUE 
         AND b.valid = TRUE 
       GROUP BY b.user_id"; 
     $sth = $this->db->prepare($sql); 
     $sth->execute([':date' => $date]); 
    } 

    $result = []; 
    while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { 
     $result[$row['user_id']] = floor($row['total']); 
    } 

    // cryptographically secure pseudo-random integer, otherwise fallback 
    $total = array_sum($result); 
    if (function_exists('random_int')) { 
     $rand = $total > 0 ? random_int(0, $total - 1) : 0; 
    } else { 
     // fallback, NOT cryptographically secure 
     $rand = $total > 0 ? mt_rand(0, $total - 1) : 0; 
    } 

    $running_total = 0; 
    foreach ($result as $user_id => $credits) { 
     $running_total += $credits; 
     if ($running_total > $rand) { 
      // we have a winner 
      return $user_id; 
     } 
    } 

    return false; 
} 

所以我基本上是我要选择多个获奖者执行该代码多次。

2

您可以通过计算累积分布和使用rand()选择一个用户:

SELECT uc.* 
FROM (SELECT uc.user_id, (@t := @t + total) as running_total 
     FROM (SELECT uc.user_id, SUM(credits) as total 
      FROM user_contribution_types ct JOIN 
       user_contributions c 
       ON ct.type_id = c.type_id 
      WHERE c.create_date >= '2017-05-01' AND ct.valid = TRUE AND c.valid = TRUE 
      GROUP BY uc.user_id 
      ) uc CROSS JOIN 
      (SELECT @t := 0) params 
     ORDER BY rand() 
    ) uc 
WHERE rand()*@t BETWEEN (running_total - total) AND running_total; 

有一个微小的机会,这将返回两个值,如果rand()正是在边界上。为了您的目的,这不是一个问题;你可以添加limit 1

对此扩展到多行,你可以简单地修改WHERE子句:

WHERE rand()*@t BETWEEN (running_total - total) AND running_total OR 
     rand()*@t BETWEEN (running_total - total) AND running_total OR 
     rand()*@t BETWEEN (running_total - total) AND running_total 

的问题是,所有的结果值可能是相同的结果。

您可以随机选择三个以上的值。我的倾向是选择一个更大的数字,如9:

WHERE 0.1*@t BETWEEN (running_total - total) AND running_total OR 
     0.2*@t BETWEEN (running_total - total) AND running_total OR 
     0.3*@t BETWEEN (running_total - total) AND running_total OR 
     . . . 
ORDER BY rand() -- redundant, but why not? 
LIMIT 3 

或者更简单地说:

WHERE FLOOR(10*(running_total - total)/@t)) <> FLOOR(10*running_total/@t) 
ORDER BY rand() 
LIMIT 3 

这是更容易,因为你可以改变10和沿测试任何数量的等距点累积分配。

+0

如果我理解正确,您的第一个示例可以与应用程序级逻辑结合使用?所以我可以将它包装到一个函数中并将结果传回给它自己?即,'WHERE user_id不在($ winners)'中。这将完全排除重复的结果... –

+0

此外,我收到此错误:'字段列表'中的未知列'uc.user_id' –

+0

@mistermartin。 。 。该列在您的问题中指定。你可能已经错过了你。user_id',但我修复了别名。 –