在sql中从数据库中选择时如何排除值

2ekbmq32  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(383)

如何从已添加到调度程序表中的主题表中排除要显示的主题?我有这个代码,但它仍然显示已添加到调度表的主题。

<?php
    $sem = $_GET['semChooser'];

    $res = mysql_query("SELECT * FROM scheduler WHERE semester = '$sem'") or die(mysql_errno());
   $row=mysql_fetch_array($res);

           $subName = $row['subject_name'];

        $res1 = mysql_query("SELECT * FROM subject WHERE semester IN ('$sem', 3) AND subject_name != '$subName'") or die(mysql_errno());
      while ($row=mysql_fetch_array($res1)) {

      echo "
         <tr class='active'> 
            <td align='center'>
              <button type='submit' data-dismiss='modal' class='btn btn-primary btn-block btn-xs subject'><em class='fa fa-plus'></em> Add</button>
            </td>
            <td>".$row['subject_code']."</td> 
            <td>".$row['subject_name']."</td> 
            <td>".$row['subject_type']."</td>
            <td>".$row['room_type']."</td>
            <td>".$row['subject_category']."</td>
            <td>".$row['year_level']."</td>  
            <td>".$row['units']."</td> 
         </tr> "; }
    ?>
f0brbegy

f0brbegy1#

您可以使用subselect并仅通过一个查询获得结果

SELECT * 
      FROM subject 
      WHERE semester IN ('$sem', 3)  
      AND subject_name not in ( SELECT * FROM scheduler 
                                WHERE semester = '$sem' )

相关问题