Select Statement正在加载错误的数据

时间:2013-03-24 22:46:12

标签: php sql oop select inner-join

我的SQL SELECT语句出了问题,我在Google和youtube上搜索但是我什么也没找到。

这是我的PHP代码:

public function loadMark($data){
    dbConn(); 

    $query = "SELECT
        subject.subName
        , mark.mark
    FROM
        itcollage.marktype
        , itcollage.student
    INNER JOIN itcollage.parent 
        ON (student.pId = parent.pId)
    INNER JOIN itcollage.mark 
        ON (mark.stId = student.sId)
    INNER JOIN itcollage.subject 
        ON (mark.subId = subject.subId)
    WHERE 
        parent.pUsername='$data' 
    AND 
        mark.subId = subject.subId LIMIT 2";

    return $result=  mysql_query($query);

}

我不想为获得学生证的父母加载正确的标记,但它会不断重复所有科目的分数。

数据如下:

Subject      Mark
--------  | ------
Java 101  |   A
Java 101  |   D
Math 101  |   A
Math 101  |   D
  • Java 101 = A,Math = D

所以错了!!!

这是我调用类marks.class.php

的代码
    include '../classes/mark.class.php';

    $mark = new mark();
    $result = $mark->loadMark($user);
    $num = mysql_num_rows($result);

     if ($num == 0) {
?>
         <h2>No Marks for your Kid</h2>
<?php
     } else {
?>
         <h2>Marks</h2>
         <table>
             <thead>
                 <tr>
                     <th>Subject</th>
                     <th>Mark</th> 
                  </tr>
              </thead>
              <tbody>
<?php
                  while ($row = mysql_fetch_object($result)) {
                      $marks = $row->mark;
                      $subject = $row->subName;
?>

                      <tr class="light">
                          <td align="center"><?php echo $subject; ?></td>
                          <td align="center"><?php echo $marks; ?></td>
                      </tr>

 <?php
                   }
 ?>
               </tbody>   
           </table>

有人能告诉我什么是错的吗?

1 个答案:

答案 0 :(得分:1)

这看起来像是问题所在。您没有为此表指定连接条件。

FROM
    itcollage.marktype
    , itcollage.student

更改为:

FROM
    itcollage.marktype 
    INNER JOIN itcollage.student *(add join condition here)*