0

我正在尝试从待处理的特定卡组 (deck_id) 中获取所有抽认卡。

抽认卡待处理时

  • 它没有经过测试,
  • next_review未设置(新卡)
  • next_review <= $today(意味着卡是由于审查)。

我目前得到的是来自不同卡片组的结果,例如deck_id 1deck_id 2但我只想从同一个卡片组(如deck_id = 1)中检索待处理的卡片。

这是我尝试过的:

//Get the untested and ready to review cards from DB
public function getCardSR($deckId)
{

    $today = date('Y-m-d');
    $notSet = '0000-00-00';

$query = "SELECT tbl_repetition.repetition_id, 
                 tbl_repetition.card_id, 
                 tbl_repetition.deck_id, 
                 tbl_repetition.user_id, 
                 tbl_repetition.status, 
                 tbl_repetition.last_review, 
                 tbl_repetition.eFactor, 
                 tbl_repetition.inter, 
                 tbl_repetition.repetition, 
                 tbl_repetition.grade, 
                 tbl_cards.front, 
                 tbl_cards.back
    FROM tbl_repetition
    LEFT JOIN tbl_cards
    ON tbl_repetition.card_id = tbl_cards.card_id
    WHERE tbl_repetition.deck_id = :deckId AND 
          tbl_repetition.next_review <= :today OR 
          tbl_repetition.next_review = :notSet";

    $stmt = $this->conn->prepare($query);
    $stmt->bindParam(':deckId', $deckId);
    $stmt->bindParam(':today', $today);
    $stmt->bindParam(':notSet', $notSet);
    $stmt->execute();

    $result = $stmt->fetchAll();

    return $result;
}   

这是从$result数组中检索结果的方式:

$repetition_id = $decks->getCardSR($c)[$repetitionCard]  ["repetition_id"];
$eFactor =       $decks->getCardSR($c)[$repetitionCard]["eFactor"];
$card_id =       $decks->getCardSR($c)[$repetitionCard]["card_id"];
$deck_id =       $decks->getCardSR($c)[$repetitionCard]["deck_id"];
$user_id =       $decks->getCardSR($c)[$repetitionCard]["user_id"];
$status =        $decks->getCardSR($c)[$repetitionCard]["status"];
$last_review =   $decks->getCardSR($c)[$repetitionCard]["last_review"];
$inter =         $decks->getCardSR($c)[$repetitionCard]["inter"];
$repetition =    $decks->getCardSR($c)[$repetitionCard]["repetition"];
$grade =         $decks->getCardSR($c)[$repetitionCard]["grade"];
$front =         $decks->getCardSR($c)[$repetitionCard]["front"];
$back =          $decks->getCardSR($c)[$repetitionCard]["back"];
4

1 回答 1

0

您的问题来自子句中的运算符优先级。WHERE

现在,您正在这样做:

WHERE tbl_repetition.deck_id = :deckId AND 
      tbl_repetition.next_review <= :today OR 
      tbl_repetition.next_review = :notSet

我们称第一个表达式为 1,第二个为 2,第三个为 3。

这意味着它将像这样评估:

  1. AND将首先被评估,所以它会做(1 AND 2)
  2. OR将被评估第二个,所以你会导致(1 AND 2) OR 3

这意味着您将从今天需要复习的一副牌中检索所有卡片,以及所有未设置复习的牌中的所有牌...

要解决这个问题,您必须放置括号以OR首先评估:

WHERE tbl_repetition.deck_id = :deckId AND 
      (tbl_repetition.next_review <= :today OR 
      tbl_repetition.next_review = :notSet)

这样你就会有你想要的

于 2016-05-26T07:12:30.637 回答