mysql删除重复的月份

时间:2017-06-06 18:38:17

标签: php mysql

我想删除重复的月份

这是我的数据 enter image description here

我想删除重复并显示如下

enter image description here

这是我的代码

<?php
    include "connect.php";
    $query = "select * from mydata";
    $result = mysqli_query($con,$query); 
    while($data  = mysqli_fetch_array($result)){
        $dbasearray[] = $data['month_data'].",".$data['week_data'].",".$data['total_data'];
    }
    $s1 = array();
    $s2 = array();
    $a  = array();
    $b  = array();

    echo "<table border='1'>

            <tr>
                <th>Month</th>
                <th>Week</th>
                <th>Total</th>
                <th>s1</th>
                <th>s2</th>
                <th>a</th>
                <th>b</th>
            </tr>
            ";
        foreach ($dbasearray as $index => $datah){

            $data = explode(",", $datah);

            echo "<tr>";
            echo "<td align='center'>".$data[0]."</td>";
            echo "<td align='center'>".$data[1]."</td>";
            echo "<td align='center'>".$data[2]."</td>";
                if($index==0){
                    echo "<td>".$s1[$index] = $data[2]."</td>";
                    echo "<td>".$s2[$index] = $data[2]."</td>";
                }
                else {
                    echo "<td>".$s1[$index] = 0.1*$data[2]+0.9*$s1[$index-1]."</td>";
                    echo "<td>".$s2[$index] = 0.1*$s1[$index]+0.9*$s2[$index-1]."</td>";
                }
                echo "<td>".$a[$index] = 2*$s1[$index]-$s2[$index]."</td>";
                echo "<td>".$b[$index] = (0.1/0.9)*($s1[$index]-$s2[$index])."</td>";
            echo "</tr>";
        }
    echo "</table>";
    ?>

我不知道删除/隐藏我的代码,我不能使用group by 帮我。感谢的

3 个答案:

答案 0 :(得分:1)

从以下内容中我应该非常清楚我没有PHP编码器,但它应该提出这个想法......

样本数据集:

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,month INT NOT NULL, user VARCHAR(12) NOT NULL
);

INSERT INTO my_table (month,user) VALUES 
(1,'Ben'),
(2,'Ben'),
(3,'Tom'),
(1,'Gary'),
(2,'Jessica'),
(3,'Dean'),
(1,'Elizabeth');

示例查询和结果:

SELECT * FROM my_table ORDER BY month,id;
+----+-------+-----------+
| id | month | user      |
+----+-------+-----------+
|  1 |     1 | Ben       |
|  4 |     1 | Gary      |
|  7 |     1 | Elizabeth |
|  2 |     2 | Ben       |
|  5 |     2 | Jessica   |
|  3 |     3 | Tom       |
|  6 |     3 | Dean      |
+----+-------+-----------+

示例PHP代码......

<?php

require('path./to/connection/stateme.nts');

$query = "SELECT month, user FROM my_table ORDER BY month , id;";

$result = mysqli_query($conn,$query);

$x = null;

echo "<table>";
while($row = mysqli_fetch_assoc($result)){
if($row['month']==$x)
  { $month = ''; }
  else
  { $month = $row['month'];}

echo "<tr><td>$month</td><td>".$row['user']."</td></tr>";

$x = $row['month'];
}
echo "</table>";
?> 

输出:

1   Ben
    Gary
    Elizabeth
2   Ben
    Jessica
3   Tom
    Dean

答案 1 :(得分:1)

mybox.mydomain.com ansible_ssh_pass={{mypassword}}

尝试上面的代码,如果问题仍然存在,请告诉我。

答案 2 :(得分:1)

一个建议是,尝试在while循环中打印内容,而不是存储在另一个变量中并多次迭代。完成后,

请执行以下操作。你不希望打印重复的月份。 所以,在while循环之前添加它。

$prev_month = '';

然后在while循环中你需要输入正确的月份。

   if ($prev_month == $row['month'])
   {
       $month = '';
   }
   $prev_month = $row['month'];

在html打印中使用$ month。

完整的代码如下。检查它是否有效。

$month = '';
$prev_month = '';
echo "<table border='3'>";
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
   echo "<tr>";
   $month = $row['month'];

   if ($prev_month == $row['month'])
   {
       $month = '';
   }
   $week = $row['week'];
   $total = $row['total'];
   $s1 = $row['s1'];
   $s2 = $row['s2'];
   $a = $row['a'];
   $b = $row['b'];

   echo "<td>";
   echo $month ;
   echo "</td>";

   echo "<td>";
   echo $week ; 
   echo "</td>";

   echo "<td>";
   echo $total ;
   echo "</td>";

   echo "<td>";
   echo  $s1 ;  
   echo "</td>";

   echo "<td>";
   echo $s2 ; 
   echo "</td>";

   echo "<td>";
   echo $a ; 
   echo "</td>";

   echo "<td>";
   echo $b ;
   echo "</td>";



   // Fill up the rest of fields and the print them
   echo "</tr>";
   $prev_month = $row['month'];

}
echo "</table>";