0

我有这个数据库: 在此处输入图像描述

其中从 1 到 28 的数字代表一个月中的某天(FEB),1 和 0 分别代表 user_bid_id 何时下班或不下班。我想选择每周(即 1 到 7、8 到 15、16 到 23 等)人数最多的一天。

我尝试了许多不同的查询。

我可以使用此功能查看当天谁有空并计算当天的人数:

function checkPublishTraining($month){
     $month = sanitize($month);

if(bidIsPublished($month)){
 if(($month === 'FEB')){
  echo'
 <table>
 <tr id="checkBidding">
 <th>1</th> 
 <th>2</th> 
 <th>3</th> 
 <th>4</th> 
 <th>5</th> 
 <th>6</th> 
 <th>7</th> 
 <th>8</th> 
  <th>9</th> 
 <th>10</th> 
 <th>11</th> 
 <th>12</th> 
 <th>13</th> 
 <th>14</th> 
 <th>15</th> 
 <th>16</th> 
 <th>17</th> 
 <th>18</th> 
 <th>19</th> 
 <th>20</th> 
 <th>21</th> 
  <th>22</th> 
  <th>23</th> 
  <th>24</th> 
  <th>25</th> 
  <th>26</th> 
  <th>27</th> 
  <th>28</th> 
  </tr>
   <tr>';
          $i = 1;

    while ( $i <= 28 ) {
       $count = mysql_query("SELECT COUNT(`$i`)as days FROM $month WHERE `$i` = '1'");
       $num = mysql_fetch_array($count);

       echo'<td>';
       $names = mysql_query("SELECT user_bid_id FROM $month WHERE `$i` = '1'");
       while($row = mysql_fetch_array($names)){
       $name = firstName_from_id($row['user_bid_id']);
       echo '<h5>'.$name.'</h5>';
                    }

        echo'<h5>'.$num['days'].'</h5>';
         $i++;

       echo '</td>';

       }

       echo'</tr></table>';
       }
          }

             }

返回此表:

在此处输入图像描述

我现在可以选择有更多会员的日子,但尝试每月至少为每个会员提供一次培训课程吗?

4

2 回答 2

1

要做到这一点,您需要对 MySQL 不支持的数据进行 UNPIVOT。因此,您可以为此创建一个视图,使用 UNION ALL(查看下面的查询)返回未透视的数据。获得此视图后,您可以执行以下操作:

SELECT myView.week, myView.day
FROM (
  SELECT week, max(total) as total
  FROM myView
  GROUP BY week) s
JOIN myView
  ON s.week = myView.week
  AND s.total = myView.total

如评论中所述,如果一周的最大值相同,您每周可能会获得 1 个以上的结果。

这是视图查询:

CREATE myView AS
SELECT 1 as week, 1 as day, SUM(`1`) as total
FROM tableName
UNION ALL
SELECT 1, 2, SUM(`2`)
FROM tableName
UNION ALL
SELECT 1, 3, SUM(`3`)
FROM tableName
UNION ALL
SELECT 1, 4, SUM(`4`)
FROM tableName
UNION ALL
SELECT 1, 5, SUM(`5`)
FROM tableName
UNION ALL
SELECT 1, 6, SUM(`6`)
FROM tableName
UNION ALL
SELECT 1, 7, SUM(`7`)
FROM tableName
UNION ALL
SELECT 2, 1, SUM(`8`)
FROM tableName
UNION ALL
SELECT 2, 2, SUM(`9`)
FROM tableName
UNION ALL
SELECT 2, 3, SUM(`10`)
FROM tableName
UNION ALL
SELECT 2, 4, SUM(`11`)
FROM tableName
UNION ALL
SELECT 2, 5, SUM(`12`)
FROM tableName
UNION ALL
SELECT 2, 6, SUM(`13`)
FROM tableName
UNION ALL
SELECT 2, 7, SUM(`14`)
FROM tableName
UNION ALL
SELECT 3, 1, SUM(`15`)
FROM tableName
UNION ALL
SELECT 3, 2, SUM(`16`)
FROM tableName
UNION ALL
SELECT 3, 3, SUM(`17`)
FROM tableName
UNION ALL
SELECT 3, 4, SUM(`18`)
FROM tableName
UNION ALL
SELECT 3, 5, SUM(`19`)
FROM tableName
UNION ALL
SELECT 3, 6, SUM(`20`)
FROM tableName
UNION ALL
SELECT 3, 7, SUM(`21`)
FROM tableName
UNION ALL
SELECT 4, 1, SUM(`22`)
FROM tableName
UNION ALL
SELECT 4, 2, SUM(`23`)
FROM tableName
UNION ALL
SELECT 4, 3, SUM(`24`)
FROM tableName
UNION ALL
SELECT 4, 4, SUM(`25`)
FROM tableName
UNION ALL
SELECT 4, 5, SUM(`26`)
FROM tableName
UNION ALL
SELECT 4, 6, SUM(`27`)
FROM tableName
UNION ALL
SELECT 4, 7, SUM(`28`)
FROM tableName
于 2013-02-13T18:16:40.113 回答
0

对每列和之后的所有行值求和,将日期的列更改为 DAYOFWEEK 以对值进行分组

于 2013-02-13T17:50:34.790 回答