如何连接2个表没有外键但相同的数据?

时间:2016-01-12 08:31:34

标签: php

是否可以加入表格,如下图所示

enter image description here

svotes中没有外键,但记录相同。在select选项标记中,我推出school_year,选项为2015-2016,2016-2017。如果我点击2016,那么另一张表svotes是否可以显示2016年是否可以?怎么样?

这是我的代码:

<select name="sy_no" onchange="getyr(this.value)">
  <option></option>
  <?php
    include('../connect.php');
    $sql2 = mysql_query("SELECT * FROM school_year");

    while ($row1=mysql_fetch_array($sql2)) {
        echo "<option value=".$row1['syearid'].">".$row1['from_year'].'-'.$row1['to_year']."</option>";
    }
  ?>
</select>

get_winner.php

<?php
  include('../connect.php');
  $no = $_GET['no'];

  //this is where I get the year
  $stud = mysql_query("SELECT * FROM  studentvotes,school_year WHERE school_year.from_year = studentvotes.year AND studentvotes.year = '$no' ") or die(mysql_error());
  echo"<center>";
  echo "<form action='' method='POST'  enctype='multipart/form-data' >";
  echo "  <table class='table table-striped table-bordered table-hover' id='dataTables-example'>";
  echo "<tr>";
  echo "<th>IDno</th>";
  echo "<th>Action</th>";
  echo "</tr>";

  while ($row=mysql_fetch_array($stud)) {
    $result = mysql_query("SELECT * FROM studenvotes,school_year WHERE   studenvotes.year = school_year.from_year") or die(mysql_error());
    $r1 = mysql_fetch_assoc($result);
?>
<tr class="headings">
<?php
  }
?>

1 个答案:

答案 0 :(得分:1)

尝试此连接查询

<select name="sy_no" onchange="getyr(this.value)">
  <option></option>
  <?php
    include('../connect.php');
    $sql2 = mysql_query("SELECT school_year.*,svotes.* FROM school_year JOIN svotes ON , svotes.year = school_year.from_year ");

    while ($row1=mysql_fetch_array($sql2)) {
        echo "<option value=".$row1['syearid'].">".$row1['from_year'].'-'.$row1['to_year']."</option>";
    }
  ?>
</select>