mySQL:如何选择每周有更多人关闭的日子

时间:2013-02-13 17:09:48

标签: mysql sql

我有这个数据库: enter image description here

其中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>';
       }
          }

             }

返回此表:

enter image description here

我现在可以选择更多会员,但每个月至少给每位会员提供一次培训课程吗?

2 个答案:

答案 0 :(得分:1)

要做到这一点,你需要UNPIVOT数据,MySQL不支持。因此,您可以使用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

答案 1 :(得分:0)

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