2013-02-11 92 views
1

我的代碼填充表是目前這樣的:PHP/MYSQL:通過柱

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  

我可以輸出的數據,因爲它是上面格式化直接從數據庫中的表,但是我想輸出作爲表,例如(由lessonsession碎):

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 

....和以此爲下一課lessonsession小組。

以前可能一直困惑。希望這有助於澄清我的問題。謝謝!

+0

爲什麼店/複製,而不是一個複雜的'SELECT'的數據? – philwinkle 2013-02-11 22:29:16

+0

複雜選擇的例子是什麼?我只使用了「select * from'studentresponse'where userid'= 8(例如) – 2013-02-11 22:37:50

+0

@ChrisWilson結果的列標題是什麼? – Taryn 2013-02-11 22:39:50

回答

0

這是一個非常醜陋的方式來做到這一點在SQL中。

這個答案實現了用戶定義的變量來爲每一行生成一個行號,這個用於unpivot的數據轉化爲列。最後,使用表達式爲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 
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>'; 

這將產生以下: enter image description here 現在,所有我需要做的就是打破錶中「lessonsession」每一個變化。

(我真的感覺像一個程序員現在 - 我想用一個分號終止的句子。)