1

我的代码目前是这样的:

if (isset($_POST['viewstudentdrop'])) {
    $query = "SELECT students.*, lessons.lessonname 
            FROM lessons INNER JOIN 
            (assignments INNER JOIN students 
            ON assignments.studentid = students.id) 
            ON lessons.lessonid = assignments.lesson
            WHERE (((students.id)=".$_POST['viewstudentdrop']."))";
    $results = $pdodl->query($query);
    while ($row = $results->fetch()) {
            echo "<p>Lesson: ". $row['lessonname'] . "</p>";
    }
   // View Completed Lessons and Times
    $query = "SELECT * 
            FROM studentresponse 
            WHERE studentid = ".$_POST['viewstudentdrop']." 
            ORDER BY lessonsession";
    $results = $pdodl->query($query);
    echo '<table> ';
    while ($row = $results->fetch()) {
    echo '<tr> <td> ' . $row['actiontime'] .'</td>' .
                     ' <td> ' . $row['page'] .'</td>' .
                     ' <td> ' . $row['response'] . '</td>'.
                     ' <td> '. $row['lessonsession'] . '<td> </tr>';
    }
    echo  '</table>';  
}

它会产生如下输出:

19:40:44     sda02   C   11360611641    
19:40:46     sda03   D   11360611641    
19:40:50     sda04   3   11360611641    
19:40:53     sda05   A   11360611641    
19:41:22     sda02   B   11360611678    
19:41:24     sda03   C   11360611678    
19:41:31     sda04   5   11360611678    
19:41:34     sda05   B   11360611678    
20:00:39     sda02   B   11360612836    
20:00:41     sda03   C   11360612836    
20:00:44     sda04   3   11360612836    
20:00:47     sda05   B   11360612836    

我可以将上面格式化的数据直接从数据库输出到表中,但我想将其输出为表,例如(被课程会话打破):

sda02     sda03     sda04     sda05
19:40:44  19:40:46  19:40:50  19:40:53
C         D         3         A

sda02     sda03     sda04     sda05
19:41:22  19:41:24  19:41:31  19:41:34
B         C         5         B

.... 等下一节课组。

之前可能一直很困惑。希望这有助于澄清我的问题。谢谢!

4

2 回答 2

0

这是在 SQL 中执行此操作的一种非常丑陋的方法。

这个答案实现了用户定义的变量来为每一行生成一个行号,然后用于将数据取消透视到列中。最后,使用带有CASE表达式的聚合函数将数据旋转回列。

select col,
  lessonsession,
  max(case when rn = 1 then value end) Col1,
  max(case when rn = 2 then value end) Col2,
  max(case when rn = 3 then value end) Col3,
  max(case when rn = 4 then value end) Col4
from
(
  select 'actiontime' col, actiontime value, lessonsession, rn
  from 
  (
    select actiontime,
      page, 
      response, 
      lessonsession,
      @row:=case when @prev=lessonsession then @row else 0 end +1 rn,
      @prev:=lessonsession
    from yourtable
    cross join (select @row:=0, @prev:=null) r
    order by lessonsession
  ) s
  union all
  select 'page' col, page value, lessonsession, rn
  from
  (
    select actiontime,
      page, 
      response, 
      lessonsession,
      @row:=case when @prev=lessonsession then @row else 0 end +1 rn,
      @prev:=lessonsession
    from yourtable
    cross join (select @row:=0, @prev:=null) r
    order by lessonsession
  ) s
  union all
  select 'response' col, response value, lessonsession, rn
  from 
  (
    select actiontime,
      page, 
      response, 
      lessonsession,
      @row:=case when @prev=lessonsession then @row else 0 end +1 rn,
      @prev:=lessonsession
    from yourtable
    cross join (select @row:=0, @prev:=null) r
    order by lessonsession
  ) s
) src
group by col, lessonsession
order by lessonsession, col

请参阅SQL Fiddle with Demo

这给出了结果:

|        COL | LESSONSESSION |     COL1 |     COL2 |     COL3 |     COL4 |
--------------------------------------------------------------------------
| actiontime |   11360611641 | 19:40:44 | 19:40:46 | 19:40:50 | 19:40:53 |
|       page |   11360611641 |    sda02 |    sda03 |    sda04 |    sda05 |
|   response |   11360611641 |        C |        D |        3 |        A |
| actiontime |   11360611678 | 19:41:34 | 19:41:31 | 19:41:24 | 19:41:22 |
|       page |   11360611678 |    sda05 |    sda04 |    sda03 |    sda02 |
|   response |   11360611678 |        B |        5 |        C |        B |
| actiontime |   11360612836 | 20:00:39 | 20:00:41 | 20:00:44 | 20:00:47 |
|       page |   11360612836 |    sda02 |    sda03 |    sda04 |    sda05 |
|   response |   11360612836 |        B |        C |        3 |        B |

您可以做的一件事是在应用行号后使用临时表记录记录。将以下内容插入临时表中,可以使代码更短而不会重复:

select actiontime,
  page, 
  response, 
  lessonsession,
  @row:=case when @prev=lessonsession then @row else 0 end +1 rn,
  @prev:=lessonsession
from yourtable
cross join (select @row:=0, @prev:=null) r
order by lessonsession
于 2013-02-11T23:53:32.133 回答
0

好的......我已经设法混淆了我最初使用数组来重新组织页面上的数据的想法。

这是代码(从第二个查询中提取):

    $query = "SELECT * 
            FROM studentresponse 
            WHERE studentid = ".$_POST['viewstudentdrop']." 
            ORDER BY lessonsession";
    $results = $pdodl->query($query);
    while ($row = $results->fetch()) { 
        $iteration[] = array ($row['page'], $row['actiontime'], $row['response'], $row['lessonsession']);
        }
    //Rewrite the data using the array into the format I want
    $x=0; $y=0;
    echo '<table>';
    while (isset($iteration[$x][$y]))  {
        echo '<tr>';        
        while (isset($iteration[$x][$y])) {
            echo '<td>' . $iteration[$x][$y] . '</td>';
            $x++;
        }
        echo '</tr>';
        $x = 0;
        $y++;
}
    echo '</table>';

这会产生以下结果: 在此处输入图像描述 现在,我所要做的就是为“课程”中的每次更改打破表格。

(我现在真的感觉自己像个程序员——我想用分号结束句子。)

于 2013-02-12T01:51:53.327 回答