简体   繁体   English

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

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

I had did the below code to show the given answer number from answer table and to show answer value and the right answer value from question bank table. 我做了以下代码来显示答案表中给定的答案号,并显示问题库表中的答案值和正确答案值。 But I need it to do the code of two distinct MySQL query in a single query some thing like this. 但是我需要它在单个查询中执行两个不同的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");

Is it possible to do the below code in a similar method like the upper one? 是否可以采用与上方法类似的方法来执行以下代码? ` `

 <?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;  
        }
     ?> 

If I am not wrong, you are just meant to do a INNER JOIN between both the tables on question_no column. 如果我没有记错的话,您只需要在question_no列的两个表之间进行INNER JOIN Something like 就像是

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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM