简体   繁体   English

MySQL GROUP by或使用PHP?

[英]MySQL GROUP by or using PHP?

I have an issue that seems to be quiet easy but I just would like to ask how you would solve it: 我有一个似乎很容易安静的问题,但我想问你如何解决它:

In a MySQL-table there is the following structure: 在MySQL表中有以下结构:

provider     artist      
a            1
a            2
a            3
b            4

Now it is necessary to echo a list in HTML like: 现在有必要回复HTML中的列表,如:

provider a
1
2
3

provider b
4

I stuck at the point where I would like to group the results and echo them out with for-each and while loop. 我坚持我想要对结果进行分组并用for-eachwhile循环回显它们。

The main idea is quiet simple like: 主要想法很简单,如:

<?php $query = mysqli->query("SELECT * FROM `table` GROUP by provider");

foreach GROUP {?>

  echo some styling HTML-code for each group of headline;

  <?php while ($data= $query->fetch_assoc()){?>

  echo some styling HTML-code for each list-item;

<?php};?>

Thanks in advance. 提前致谢。

UPDATE: 更新:

Thanks for answering. 谢谢回答。

The solution fro RiggsFolly seems to work fine. RiggsFolly的解决方案似乎运行良好。 There is just a small problem with the HTML. HTML只有一个小问题。 There is a surrounding div-tag that wont be closed when adding the HTML code in that line: 在该行中添加HTML代码时,周围的div标签不会被关闭:

 echo 'provider '. $data->provider;

The problem is that the while loop needs to be in the div. 问题是while循环需要在div中。 The closing div-tag is missing for every 每个人都缺少关闭的div-tag

if ( $current_provider != $data->provider ) {

Here is the original HTML-code: 这是原始的HTML代码:

    <?php
        $service = $db->query("SELECT * FROM `system` ORDER BY provider, artist");
        $current_provider = NULL;

        while ($data = $service->fetch_object()) {
            if ( $current_provider != $data->provider ) {// new provider?>
    <div class="service">
    <p class="lower">
     <?php echo $data->provider;?>
    </p>
    <?php
        $current_provider = $data->provider;
    }?>
    <a href='?artist=<?php echo $data->artist;?>'><?php echo "/".$data->artist;?</a><br/>   
    <?php };?>
</div><!--service -->

The list-items will be shown nicely. 列表项目将很好地显示。 But when looking into the Source code you can see that the closing div-tag is missing. 但是在查看源代码时,您可以看到关闭的div-tag丢失了。 Thanks 谢谢

Kind regards. 亲切的问候。

It would seem simpler to not use a GROUP BY especially as it will not provide you with the data that you want. 不使用GROUP BY似乎更简单,因为它不会为您提供所需的数据。 So instead just select them all and sort them by provider and maybe artist as a sub sort like so 因此,只需选择它们并按提供者对它们进行排序,也可以将艺术家作为子类进行排序

<?php 
    $result = $mysqli->query("SELECT * FROM `table` ORDER BY provider, artist");

    $current_provider = NULL;

    while ($data = $result->fetch_object()){
        if ( $current_provider != $data->provider ) {
            // new provider
            echo 'provider '. $data->provider;
            $current_provider = $data->provider;
        }
        echo $data->artist;
    }
?>

AFTER UPDATE: 更新后:

<?php
    $service = $db->query("SELECT * FROM `system` ORDER BY provider, artist");

    $current_provider = NULL;

    while ($data = $service->fetch_object()) {

        if ( $current_provider != $data->provider ) {
            if ( $current_provider !== NULL ) {
                echo '</div>';
            }
            echo '<div class="service">';
            echo '<p class="lower">' . $data->provider . '</p>';
            $current_provider = $data->provider;
        }
        echo '<a href="?artist=' . $data->artist '">' .
             $data->artist . '</a><br/>';   
    }
    echo '</div>';

How about that. 那个怎么样。

<?php $query = mysqli->query("SELECT * FROM `table`");
    $current_group = array();
   while ($data= $query->fetch_assoc()){
      if(in_array($data['provider'],$current_group)){
         echo  "<h1>New Group" . $data['provider'] ."</h1>";
         $current_group[] = $data['provider']
      }
     echo $data['artist'] . "<br/>";
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM