2016-09-30 80 views
2

有兩個表格。一個表格包含玩家信息,如名字和等級。另一張表包含遊戲替代信息。如何拼合MySQL結果

輸出應該是這樣的:

Rank| Player Name | Period 1 position | Period 2 Position| Period 3 Position | 

球員表取樣

|ID| Name | Rank | 
|1 | Fred | 10  | 
|2 | Dan | 12  | 
|3 | Mike | 15  | 

位置表樣品

|ID| Game_ID | Player_ID | Period | Position  | 
|1 | 12  | 1   | 1  | Striker Right| 
|2 | 12  | 1   | 2  | Mid Center | 
|3 | 12  | 2   | 1  | Striker Left | 

下面的代碼把它弄出來的MySQL

我試過各種方法來變換返回的數據集。

$players_sql = "SELECT position.id as position_id, 
player.rank, 
player.first_name, 
position.period, 
position.position 
FROM player 
INNER JOIN position 
on player.id = position.player_id 
ORDER BY player.rank, position.period;"; 

if ($result = $mysqli->query($players_sql)) { 
// printf("Select returned %d rows.\n", mysqli_num_rows($result)); 
/* fetch associative array */ 
while ($row = $result->fetch_assoc()) { 
    $subs[$row["position_id"]] = [ 
     "rank" => $row["rank"], 
     "first_name" => $row["first_name"], 
     "period" => $row["period"], 
     "position" => $row["position"] 
    ]; 
} 
mysqli_free_result($result); 
} 

// collapse players This works 
foreach ($subs as $sub) { 
    $players_by_rank[$sub["rank"]] = $sub["first_name"]; 
} 

後來的數據是數據以用戶可以提交的形式顯示。下面的代碼與上面的代碼不匹配。下面的代碼使用靜態定義的數組,其中預填充的數據不是來自數據庫。它提供了一種可視化輸出結果的方式。在下面的代碼示例中,總共有4個週期分爲4列,最後是總結列。必須有一個簡單的方法來壓扁這些數據!

<?php 
for ($sub = 1; $sub < $sub_info . length; $sub++) { 
    ?> 
    <tr> 
     <td><?php echo $sub_info[sub] ?></td> 
     <td><input type = "text" 
        name = "p<?php echo $player; ?>" 
        value = "<?php echo $names[$player - 1]; ?>" 
        id = "p<?php echo $player; ?>" ></td> 
      <?php 
      for ($period = 1; $period < $periods + 1; $period++) { 
       ?> 
      <td> 
       <div class="form-group"> 
        <select onchange="calTotals(<?php echo $player . ',' . $period; ?>)" 
          id="<?php echo "p{$player}p{$period}"; ?>" 
          name="<?php echo "p{$player}p{$period}"; ?>"> 
           <?php 
           $output = ""; 
           for ($i = 0; $i < $players_on_field + 1; $i++) { 
            $output = "<option"; 
            if ($i == 0) { 
             $output .= ' selected'; 
            } 
            $output .= ">$i</option>"; 
            echo $output; 
           } 
           ?> 
        </select> 
       </div> 
      </td> 
     <?php } ?> <!-- Periods --> 
     <td><!-- total periods in game using JavaScript --> 
      <input type="text" 
        name="<?php echo "periods{$player}"; ?>" 
        id="<?php echo "periods{$player}"; ?>"> 
     </td> 
    </tr> 
<?php } ?> <!-- players --> 
+0

我覺得你就要成功了。 $期間在哪裏定義? – Strawberry

回答

2

您可以使用join連接表,球員表的一個實例,和三個位置(一個每週期)

SELECT pl.rank, pl.name, 
    po1.position as period_1_position, 
    po2.position as period_2_position, 
    po3.position as period_3_position from 
    player as pl 
    inner join position as po1 on pl.id = po1.player_id and po1.period=1 
    inner join position as po2 on pl.id = po2.player_id and po2.period=2 
    inner join position as po3 on pl.id = po3.player_id and po3.period=3 
+0

這回答了這個問題!謝謝!後來我不知道有多少個時期,這個時期的範圍很可能是1到8.這不是原始問題的一部分。謝謝你的直接回答! – geeves