dongxia2030 2018-06-14 12:24
浏览 123
已采纳

为什么SQL INNER JOIN返回所有记录而不考虑条件

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.

  • 写回答

2条回答 默认 最新

  • dongqianwei6664 2018-06-14 14:21
    关注

    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 where, $userRow['rollno'] is a session variable which contains the value of roll no. of currently logged in user.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误