1

我问了一个类似的问题,但是无法完全让查询给我想要的结果,它输出了一个问题,但不是正确的问题。我将在这里更好地澄清这个问题,并对表格进行一些修改。我有一个程序,如果一个人键入“让我们谈谈”,该程序会从表格中向用户询问一些问题(例如 20 qns)

我有 3 张桌子。

   (peopleiknow)           (questions)              (questionsasked)    
    NID    FName            QID   Question             QID PID Answer

     1      Mark             1   Are you human          1    1 'Definately yes.'
     2      Sue              2   Do you like soup?      1    2 'Most of the time.'
                                                        2    2 'Yes especially tomato.'

(*问题表一开始可以为空)

基本上,该程序会为不在 questionsasked 表中的用户“$name”获取第一个问题。然后提出问题并将答案添加到问题表中。(起初我试图得到正确的问题)

与 php 交互的 javascript/jQuery 片段如下:

    var uname='Lindsay';  //will be assigned dynamically after I get this fixed.  
 $.post('ajax/getQuestions.php', { name: uname },function(data2) {
   var qn = data2.value1; //question
   var pid = data2.value2; // PID
   var qid = data2.value3;  //QID
   alert(qn);
   alert(pid);
   alert(qid);
 var answer1 = prompt(qn, 'Please answer here');
 $.post('ajax/addQuestionAsked.php', {answer: answer1, PID: pid, QID: qid},
       function(data3) {alert('added question');} );


  //$('div#PlaceOfResponse').html(data3);}

  });

PHP文件的重要部分如下:

*连接部分省略 * $link 是连接

 if (isset($_POST['name']) && !empty($_POST['name'])) 
 {
    $name = trim($_POST['name']); //could do better protection..

  $query1 = "SELECT *      //(q.Question, q.QID, p.person)?                         
  FROM questions q
  INNER JOIN questionsasked qa 
  ON q.QID = qa.QID 
  INNER JOIN peopleiknow p
  ON qa.PID = p.NID
  WHERE qa.Asked='N' OR qa.Asked IS NULL
  AND p.Person = '$name'"; 

上面的查询不太奏效,也尝试了下面评论中的那个..但没有奏效..

/*
         $query1 = "SELECT FIRST(Question)
                    FROM questions q
                    WHERE NOT EXISTS
                      (SELECT * 
                       FROM questionsasked qa
           RIGHT JOIN peopleiknow p
                       ON p.NID = qa.PID
                       INNER JOIN questions q
                        ON qa.QID = q.QID
                        WHERE q.QID = qa.QID
                        AND p.FName = '$name')";                  
        */


  if ($result = mysqli_query($link, $query1)) 
  {
      if (0 == mysqli_num_rows($result)) 
  {
       echo('all asked');
   }
  else
   {
     /* fetch associative array */
       $row = mysqli_fetch_assoc($result);
     {header('Content-Type: application/json');
    $question = $row['Question'];
    $PID = $row['PID'];  
    $QID = $row['QID'];
        $response = array('value1' => $question, 'value2' => $QID, 'value3' => $PID);
          echo json_encode($response);
         }
     }  
 }
 }

 else {
 ?><script>alert('$name');</script>
<?php
echo 'variable \'name\' not set properly';}
 /* close connection */
 mysqli_close($link);

 ?>

我几乎可以正常工作,但是当我将答案添加到问题表中,并刷新并说“让我们谈谈”时,它又问了同样的问题。然后我稍微更改了代码,现在 js 文件中的警报现在正在为所有 3 个发回的变量发出警报(未定义)。(在我稍微修改表之前工作)。现在看不出它为什么这样做。

这是我对查询的最新尝试:

  $query1 = "SELECT FIRST(q.question,p.NID,q.QID)                               
  FROM questions q
  LEFT JOIN questionsasked qa 
  ON q.QID = qa.QID 
  LEFT JOIN peopleiknow p
  ON qa.PID = p.NID
  WHERE qa.QID
  NOT IN (SELECT * FROM questionsasked WHERE q.QID = qa.QID) 
  AND p.FName = '$name'";
4

1 回答 1

1

您可能需要考虑阅读左连接的工作原理

尝试这样的事情:

"SELECT *      
  FROM questions q
  LEFT JOIN questionsasked qa 
  ON   q.QID = qa.QID 
   and qa.PID = (select NID from peopleiknow where FName = '$name')
  WHERE qa.PID IS NULL "

这次我用 SQLite DB 验证了它:-) 由于左连接中的missin PID,以前的版本不起作用!

问候,

马丁

于 2013-01-11T11:55:49.077 回答