连接两个 MySQL 表,其中连接条件来自其中一个表


Joining two MySQL tables where the join condition is from one of the tables

我做了下面的代码来显示答案表中给定的答案编号,并显示答案值和问题库表中的正确答案值。但是我需要它在单个查询中执行两个不同的MySQL查询的代码,例如这样的事情。

$result = mysql_query("SELECT t1.*, t2.(t1.clicked_answer)
                        FROM Eg_Net_Solution_test_answer AS t1,                                                 
                             Eg_Net_Solution_test_question_bank AS t2
                        WHERE t1.user_serial = '10' AND 
                              t1.area='Hamdun Pur' AND
                              t1.question_no=t.question_no");

是否可以以类似于上面的方法执行以下代码?`

 <?php
  $given_answer_value="";
  $right_answer_value="";
  $question_no="";
  $given_answer_no="";
  $right_answer_no="";
 $result=mysql_query("SELECT * from  Eg_Net_Solution_test_answer where user_serial='10' AND  area='Hamdun Pur'" );
  while($row=mysql_fetch_array($result))
         {
            $question_no=$row['question_no'];//question_no is a Column of Eg_Net_Solution_test_answer table 
            $given_answer_no= $row['clicked_answer']; //clicked_answer is a Column of Eg_Net_Solution_test_answer table contains value a,b,c,d
            $result2=mysql_query("SELECT * from  $Eg_Net_Solution_test_question_bank where question_no='$question_no'" );
            while($row2=mysql_fetch_array($result2))
              {
                $given_answer_value=$row2[$given_answer];// $given_answer is Column of Eg_Net_Solution_test_question_bank table and contains string value. Like $given_answer=a, and this colun a contains value "Prophet Muhammad RIP"
                $right_answer_value=$row2[right_answer];// right_answer is Column of Eg_Net_Solution_test_question_bank table contains vale a,b,c,d
              }
        echo $row['question_no'];  
        echo "(".$row['answer']."). ".$given_answer_value;  
        echo "(".$row['right_answer']."). ".$right_answer_value;  
        }
     ?> 
如果我

没记错的话,你只是想在question_no列的两个表之间做一个INNER JOIN。类似的东西

SELECT t1.*, t2.clicked_answer 
from  Eg_Net_Solution_test_question_bank t1 
join Eg_Net_Solution_test_answer t2 on t1.question_no = t2.question_no
where t2.user_serial = '10' 
and  t2.area = 'Hamdun Pur';