简体   繁体   English

在PHP中按天对MySQL数据进行分组

[英]Group MySQL data by day in PHP

$query="SELECT * FROM cse_routine WHERE batch ='$batch' and section='$section'  order by 'day'";
$post = $db->select($query);
?> 

<?php
  if ($post) {
    while ($result = $post->fetch_assoc()) {
      $var = "select * from cse_routine where day = '".$result['day'] ."'";
      print $result['day']."<br>";
        if ($results=$db->select($var))
          {
             while ($rows = mysqli_fetch_assoc($results))
               {
                  print $rows['t_slot']. " " . $rows['room']. " " . $rows['day']. " " . $rows['c_code'];
                  echo "<br>";
               }
          }
          echo "<br>";
        }
  }
  echo "<br>";  
?>

Running this code; 运行这段代码; I found this: 我找到了这个:

码

It's ok. 没关系。 But actually, I want output that not print the same date repeatedly. 但是实际上,我想要的输出不会重复打印相同的日期。 See here Saturday, Sunday print 3 each of them but I want that Saturday print 1 and Sunday print for 1. 看到这里星期六,星期日打印3个,但我希望星期六打印1和星期日打印1个。

You just change you query to 您只需将查询更改为

$query="SELECT * FROM cse_routine WHERE batch ='$batch' and section='$section'

  GROUP by 'day'";

You could try using DISTINCT constraint ( http://www.mysqltutorial.org/mysql-distinct.aspx ) in first SQL: 您可以尝试在第一个SQL中使用DISTINCT约束( http://www.mysqltutorial.org/mysql-distinct.aspx ):

$query="SELECT DISTINCT day  FROM cse_routine WHERE batch ='$batch' and section='$section' order by 'day'";

So you will have Saturday and Sunday only once each. 因此,每个星期六和星期日只有一次。

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

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