简体   繁体   中英

Why SQL INNER JOIN returns all records regardless of condition

I have two tables users and test . The users table contains the information about student and test table has its test details. I want to show all the details of test to the specific student after login according to his roll number. For achieving this, I have tried using following foreach function and INNER JOIN in the query.

<?php 
      foreach($conn->query('SELECT test.date, test.sub,test.topic,test.marks,test.rank FROM test JOIN users ON test.rollno=users.rollno order by date desc')as $row){ ?>
        <button class="accordion"><b>Test on <?php echo date('d-m-Y', strtotime( $row['date'] )); ?></b></button>
          <div class="panel">
           <p>
           <table border="1">
            <?php
            echo "<tr><td>Date: " . date('d-m-Y', strtotime( $row['date'] )) . "</td></tr>";
            echo "<tr><td>Subject: " . $row['sub'] . "</td></tr>";
            echo "<tr><td>Topic: " . $row['topic'] . "</td></tr>";
            echo "<tr><td>Marks: " . $row['marks'] . "</td></tr>";
            echo "<tr><td>Rank: " . $row['rank'] . "</td></tr>"; ?>
         </table>
            </p>
          </div>
      <?php }?>   

This returns all the test records regardless of roll no. I have rollno column common in both table. I want the query to show records matching the current roll number. Please help.

If i have understood you want to select for one user but you don't filter on him:

SELECT test.date,
       test.sub,
       test.topic,
       test.marks,
       test.rank
FROM test
JOIN users USING(rollno)
WHERE rollno=yourusernumber
ORDER BY date DESC

It solved by just changing the query as SELECT test.date, test.sub,test.topic,test.marks,test.rank FROM test JOIN users USING(rollno) WHERE rollno='.$userRow['rollno'].' order by date desc SELECT test.date, test.sub,test.topic,test.marks,test.rank FROM test JOIN users USING(rollno) WHERE rollno='.$userRow['rollno'].' order by date desc where, $userRow['rollno'] is a session variable which contains the value of roll no. of currently logged in user.

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