0

我的数据库系统非常简单。我与用户有一张桌子,还有一张带有“猜测”的桌子。我的网站有徽标,用户可以猜出每个徽标的名称。logo_id和之间存在独特的关系user_id。这是猜测表结构:

|| id | user_id | logo_id | guess | guess_count | guessed | level | time ||

现在,当用户访问该页面但未登录时,用户会使用session_id并且所有猜测和内容都存储在相同的结构中。所以唯一的区别在于user-table。

现在我希望用户能够登录并保留他/她刚刚所做的一切。当然,如果登录的帐户已经有猜测的徽标,则不应更改。但是当登录帐户有一个未猜测的标志时,guess_count会话用户的 应该添加到登录用户中guess_count。也guessed应该更新。只有在编辑时间较新时才应该这样做,但我认为可以安全地假设会话猜测是较新的。

现在我将如何做到这一点,首先遍历登录 id 中的所有徽标 where guessed = 0,然后对每个结果再次执行查询以添加guess_count并存储guessed,然后删除从 session-id 中找到的所有徽标,然后循环通过所有具有 session-id 的旧的并将其更改user_id为登录用户之一。现在,这是一吨的查询,所以必须有一个更有效的方式。

有什么想法吗?抱歉,文字墙和不好的解释,数据库不是我最好的东西。

4

2 回答 2

1

抱歉,驴子几年没用过 mySQL,但这里有一个 SQL Server 中存储过程的例子:希望有人可以帮助了解 MySQL 的语法,或者你可以从下面的 SQL 中推断出来

CREATE PROC MergeGuesses
 @UserSessionId INT,
 @UserId INT
AS

 --where the userId has already a guess for the logo update it
 UPDATE gusr
 SET gusr.guess_count = gusr.guess_count + gses.guess_count,
     gusr.guessed = gses.guessed
 FROM Guesses gusr
 JOIN Guesses gses ON gusr.logo_id = gses.logo_id
                   AND gusr.time > gses.time  -- more recent
 WHERE gusr.user_id = @UserId
 AND gses.user_id = @UserSessionId
 AND gses.guessed = 0 --not yet guessed

 --where there is no guess for the user yet - just update the userId
 UPDATE gses
 SET gses.user_id = @UserId
 FROM Guesses gses
 LEFT JOIN Guesses gusr ON gusr.logo_id = gses.logo_id
                        AND gusr.user_id = @UserId
 WHERE gses.user_id = @UserSessionId
 AND gusr.user_id = NULL -- there is no guess for the userId

 --finally delete any rows for the sessionId that are left
 DELETE FROM Guesses 
 WHERE user_id = @UserSessionId
于 2013-07-22T11:15:38.390 回答
0

由于我不知道如何启动存储过程,所以我只是在几个查询中将其写出来,但我想没关系。

$STH = $DBH->prepare("SELECT logo_id, guess_count, guessed, guess FROM guess WHERE user_id=:id GROUP BY logo_id");
$STH->bindParam(":id",$loginuser['id']);
$STH->execute();
while($row = $STH->fetch()){
    if($row['guessed'] == 0){
        $STH2 = $DBH->prepare("SELECT guess, guess_count, guessed FROM guess WHERE logo_id=:logo_id AND user_id=:id");
        $STH2->bindParam(":logo_id",$row['logo_id'],PDO::PARAM_STR,20);
        $STH2->bindParam(':id',$_SESSION['login'],PDO::PARAM_INT);
        $STH2->execute();
        $row2 = $STH2->fetch(PDO::FETCH_ASSOC);
        if($row2){
            $STH3 = $DBH->prepare("UPDATE guess SET guess_count=guess_count+:guess_count, guessed=:guessed, guess=:guess WHERE logo_id=:logo_id AND user_id=:id");
            $data = array('guess_count'=>$row2['guess_count'],'guessed'=>$row2['guessed'],'guess'=>$row2['guess'],'logo_id'=>$row['logo_id'],'id'=>$loginuser['id']);
            $STH3->execute($data);

            $STH3 = $DBH->prepare("DELETE FROM guess WHERE logo_id=:logo_id AND user_id=:id");
            $STH3->bindParam(":logo_id",$row['logo_id']);
            $STH3->bindParam(':id',$_SESSION['login']);
            $STH3->execute();
        }
    }else{
        $STH2 = $DBH->prepare("DELETE FROM guess WHERE logo_id=:logo_id AND user_id=:id");
        $STH2->bindParam(":logo_id",$row['logo_id']);
        $STH2->bindParam(':id',$_SESSION['login']);
        $STH2->execute();
    }
}
$STH = $DBH->prepare("UPDATE guess SET user_id=:login WHERE user_id=:session"); // update all entries that are new from session
$STH->bindParam(':login',$loginuser['id']);
$STH->bindParam(':session',$_SESSION['login']);
$STH->execute();
于 2013-07-29T23:50:22.737 回答