mysql连接两表查询

1bqhqjot  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(266)

我有两个表,即抽象表和author与一对多的关系,对于while循环中的每个迭代,我想显示一个html表,其中包含抽象表中唯一的数据行和author表中相应的行。
我就是这么做的:

public function getAll() {
    try {
          $sql = "  SELECT tbl_abstract.abstract_id, tbl_abstract.first_name,
                            tbl_abstract.last_name,tbl_abstract.content,        
                            tbl_author.afirst_name, tbl_author.alast_name, 
                            tbl_author.aaffilition 
                    FROM tbl_abstract  
                        INNER JOIN tbl_author ON  tbl_abstract.abstract_id = tbl_author.abstract_id 
                    GROUP BY tbl_abstract.abstract_id";

          $stmt= $this->pdo->prepare($sql);
          $stmt->execute();
          $count =  $stmt ->rowCount();
          while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
          ?>

             <table class="table" >
              <tr>
                <td  align="center" >
                   //data from tbl_abstract
                   <?php echo $row["abstract_id"];  ?>. <?php print($row["abstract_title"]); ?>
                    <?php echo $row["first_name"].'&nbsp;'.$row["last_name"]; ?>,

                    //data from tbl_author
                    <?php echo $row["afirst_name"].'&nbsp;'.$row["alast_name"];?>         
                </td>  
              </tr>
              <tr>
                <td align="center" ">
                   //data from tbl_abstract
                   <?php print($row["content"]); ?>
                </td>
              </tr>

            </table>
          <?php 
         }          
    }catch(PDOException $e){
       echo $e->getMessage(); 
       return false;
    }  
}

tbl\u author中有三条记录与tbl\u abstract中的abstract\u id关联,但我只得到一条记录,而不是其中的三条。请帮助

h9vpoimq

h9vpoimq1#

试试这个:-

<?php

$sql = "SELECT * 
        FROM tbl_abstract
        where abstract_id IN (SELECT distinct abstract_id
                              FROM tbl_abstract)";

      $stmt= $this->pdo->prepare($sql);
      $stmt->execute();
      $count =  $stmt ->rowCount();
?>
      <table class="table" >
      while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
      ?>
          <tr>
            <td  align="center" >
               //data from tbl_abstract
               <?php echo $row["abstract_id"];  ?> <?php print($row["abstract_title"]); ?>
                <?php echo $row["first_name"].'&nbsp;'.$row["last_name"]; ?>,
         <?php          
         $sql1 = "SELECT *
                  FROM tbl_author
                  WHERE abstract_id = '".$row["abstract_id"]."'" ;
         $stmt1= $this->pdo->prepare($sql1);
         $stmt1->execute();
         while($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)){
                //data from tbl_author
                echo $row1["afirst_name"].'&nbsp;'.$row1["alast_name"];
         } ?>         
            </td>
            <td align="center">
               //data from tbl_abstract
               <?php print($row["content"]); ?>
            </td>
          </tr>
       <?php } ?>

        </table>
bvjveswy

bvjveswy2#

尝试删除 GROUP BY 像这样的条款:

SELECT 
    tbl_abstract.abstract_id, tbl_abstract.first_name, 
    tbl_abstract.last_name,tbl_abstract.content, tbl_author.afirst_name, 
    tbl_author.alast_name, tbl_author.aaffilition 
FROM 
    tbl_abstract  
INNER JOIN 
    tbl_author ON  tbl_abstract.abstract_id = tbl_author.abstract_id

groupby是按字段abstract\u id对所有作者进行分组,这意味着它不会返回所有作者,而是只返回一对一的abstract\u id(基于排序字段,在本例中它可能是主键,因为它没有显式定义)。。

相关问题