簡體   English   中英

PHP mySQL-如何在使用IN后從行獲取數據

[英]PHP mySQL - How to get data from row after using IN

我如何進行查詢以獲取while循環內的answer_choice訪問權限。

在這種情況下,我有兩個表:

af_questions 
    question_id

af_answers
    answer_from // user id
    answer_to  // question id 
    answer_choice // user choice 

PHP:

$questions = $db->prepare('SELECT * FROM af_questions WHERE question_id IN (SELECT answer_to FROM af_answers WHERE answer_from = :user_id)');
    $questions->bindValue(':user_id', $user_id, PDO::PARAM_INT);
    $questions->execute();

   while ($row = $questions->fetch()) {
        $question_id = $row['question_id'];
   }

我不確定如何回答您提出的問題; 這個問題對我來說沒有意義。

在我看來,您真正想要的是這樣的查詢結果:

  SELECT q.question.id
       , a.answer_choice
    FROM af_questions q
    JOIN af_answers a
      ON a.answer_to = q.question_id
   WHERE a.answer_from = :user_id
   ORDER BY q.question_id

然后,在訪answer_choice循環中,您將可以同時訪問question_idanswer_choice


我很確定這不會回答您提出的問題。 但是,如果我們可以從中進行備份,我很確定這種方法將幫助您實現目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM