【问题标题】:Working with two tables and grouping in mysql and php使用两个表并在 mysql 和 php 中进行分组
【发布时间】:2014-05-19 12:18:45
【问题描述】:

我有两张这样的桌子。

    books
    =====
    b_id    a_id    book_name
    1       1       ABC
    2       1       DEF
    3       2       GHI


    authors
    =======

    a_id    author_name
    1       A1
    2       A2

我需要一张这样的桌子。

        S.No    BookName
        -----------------

        A1
        ==
        1       ABC
        2       DEF


        A2
        ==

        1       GHI

我打算做的是 1. 做一个while循环,先获取作者名,打印出来 2. 在第一个循环中使用作者 id 并进行另一次迭代以获取每个作者的书单列表。

示例代码如下:

    <table cellpadding="0" cellspacing="0" border="0">
      <thead>
        <tr>
          <th>S.No</th>
          <th>Book Name</th>
        </tr>
      </thead>
      <?php
    $mysql_query1 = "my sql query to get the author name list first";
    $results = mysql_query ( $mysql_query1 ) or die ( mysql_error () );
    while($row = mysql_fetch_array($results)){
    ?>
      <tbody>
        <tr>
          <td style="background:none;"><u><?php echo $row['author_name']; ?></u></td>
        </tr>
        <?php
    $result = mysql_query ( "mysql query to get the list of books for each author with a where condition like this where a_id=$row['a_id']" ) or die ( mysql_error () );
    $book_count = mysql_num_rows($result);
    while($row1 = mysql_fetch_array($result)){
    ?>
        <tr>
          <?php for($i=1;$i<=$book_count;$i++){ ?>
          <td><?php echo $i; ?> </td>
          <td><?php echo $row1['book_name']; ?> </td>
          <?php } ?>
        </tr>
        <?php 
    }
    ?>
      </tbody>
      <?php } ?>
    </table>

我的朋友坚持我说上面的方法是老方法了,和几行代码有关。是吗?

如果是,有人可以重新定义我的代码并给我。

谢谢, 金兹

【问题讨论】:

  • 嵌套循环中的 SQL 效率极低,通常表现出对 SQL(结构化查询语言)缺乏了解。您将希望使用简单的连接来执行一个查询。
  • 那么,你想告诉什么。我已经清楚地解释了我是新手,但我们都处于学习曲线之下。对吗?
  • 当然,我没有尝试,我说您应该使用连接来一次性获取所有数据。请参阅此处以获取好的示例:stackoverflow.com/questions/12475850/…
  • 哦,顺便说一句,反对票不是来自我。

标签: php mysql arrays while-loop group-by


【解决方案1】:

这是这样吗?

$mysql_query1 = "select * from authors"

$result = mysql_query ( "select * from books where a_id=".$row['a_id']) or die ( mysql_error () );

【讨论】:

  • 这看起来不像是一个答案。
【解决方案2】:

PHP 方法类似于:

//First let's get all the values we need and store them in array format
// so that we don't need to make expensive calls to the database
<?php
$result = mysql_query("SELECT author_name, book_name FROM books b, authors a WHERE b.a_id=a.a_id");
$arr = array();
while($row=mysql_fetch_assoc($query)){
      //storing books by author
      $arr[$row['author_name']][]=$row['book_name'];
    }
//so now we have an array like ['A1'=>'ABC','DEF'],['A2'=>'GHI']
?>
<table cellpadding="0" cellspacing="0" border="0">
  <thead>
    <tr>
      <th>S.No</th>
      <th>Book Name</th>
    </tr>
  </thead>
  <tbody>
    <?php
         //let's print out the contents of our array in table format
         foreach($arr as $author=>$val){
             echo "<tr>
                      <td style='background:none;'><u>".$author."</u></td>
                      <td></td>
                   </tr>";
             foreach($val as $ind=>$book_name)
             {
               echo "<tr>
                         <td>".$ind."</td>
                         <td>".$book_name."</td>
                    </tr>";
             }
         }
      ?>
  </tbody>
</table>


注意:

Please, don't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解 prepared statements,并使用 PDOMySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial

【讨论】:

    猜你喜欢
    • 2012-05-28
    • 1970-01-01
    • 2017-04-01
    • 2014-04-11
    • 2011-10-09
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    相关资源
    最近更新 更多