SQL使用最少量的查询合并行

时间:2013-07-22 10:34:38

标签: mysql sql

我的数据库系统非常简单。我有一个用户表和一个'猜猜'表。我的网站有徽标,用户可以猜出每个徽标的名称。 logo_iduser_id之间存在唯一关系。这是猜测表结构:

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

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

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

现在我将如何执行此操作,首先在guessed = 0中循环浏览登录ID中的所有徽标,然后为每个结果再次执行查询以添加guess_count并存储{{ 1}},然后删除session-id中找到的所有内容,然后使用session-id循环遍历所有旧的,并将guessed更改为登录用户之一。现在,这是一大堆查询,因此必须有更有效的方法。

有什么想法?对不起,文字墙和&糟糕的解释,数据库不是我最好的事情。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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();