简体   繁体   中英

INNER JOIN SQL QUERY

I'm having a simple problem with this INNER JOIN query... I have a database, named BCC. I have 2 tables, question and subject. In the question table, it includes the course and cat(categories) and question. In the subject table, only ID and subject(same as the category but different field name).

Basically, I fetch all the questions in the question database, but I want them to arranged according to course and cat/subject. I have a accordion style for this feature. The link below, is the screenshot of the display output on this query

SELECT `question` FROM question WHERE `course`=$course AND `cat`=$cat;

But I look for the INNER JOIN query, and I don't even know which part I get wrong or did I forgot some code to execute the query. I think I scramble them too much that's why I got confused. Here's the SQL Query.

SELECT `question` FROM question WHERE `course`=$course AND `cat`=$cat INNER JOIN `subject` ON `subject` WHERE `subject`=`cat`

Any help is appreciated. Thank you!

I found a answer on my question last night and it is really working :))

<?php  
    $subject = $subjectRows['subject']; // subject.subject (MATH, ENGLISH, etc)
    $con=mysqli_connect("localhost","root","","bcc");

       if (mysqli_connect_errno()) 
       { 
         echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
       }

       $questionResult = mysqli_query($con,"SELECT * FROM question WHERE `cat` = '" . $subject . "' AND `course` = '". $_SESSION['SESS_COURSE'] ."' ");

      while($questionRows = mysqli_fetch_array($questionResult, MYSQL_BOTH)) 
      {  
      echo $questionRows['question'].'?<br>';
      $qid = $questionRows['qid'];
      echo '<input type="hidden" name="qqqq[]" value="'.$qid.'" />';
      echo '<select name="answer[]">';
      echo '<option>Select Answer></option>';
      $resultik = mysql_query("SELECT * FROM choices WHERE question='$qid' ORDER BY RAND() LIMIT 3");
      while($rowik = mysql_fetch_array($resultik))
      {
        echo '<option>';
        echo $rowik['opt'];
        echo '</option>';
      }
      echo '</select><br><br>';
    } 
      mysqli_free_result($questionResult);     
?>

Instead of using INNER JOIN in my query, I execute another query and used the variable.

You have to use ON with some matching condition. Your query should go like this:

SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition1

select 
    `question` 
FROM question
INNER JOIN `subject` ON --- keep join condition
WHERE `course`=$course AND `cat`=$cat and `subject`=`cat`

Please use alias with your tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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