2016-04-28 91 views
0

我有一個表,它看起來像插入列值多行到一行

hop_id hop_name site_name 
1  abcd  ctg 
2  bcss  ctg 
3  cdhj  ctg 
4  dght  dhk 
5  efgh  dhk 

我想執行一個SQL查詢返回這樣的結果:

hop_id hop_1 hop_2 hop_3 hop_4 hop_5.... site_name 
1  abcd bcss cdhj      ctg 
2  dght efgh       dhk 

有什麼想法如何寫適當的SQL爲它?任何提示將不勝感激。

+0

谷歌: 「MySQL的動態支點」。 –

回答

0

我解決了我的問題,如dis ...謝謝你的幫助。

路由路徑:

        <?php 
            //divide route path 

             echo "<br>" .$route_path."</br>"; 
             $hop = explode(">",$route_path); 
             $hop_count = count($hop)-1; 

             //maximum number of path columns in the database 
             $maxPath=9; 

             $lastIndex = 0;     
             for($i=0; $i<=$maxPath; $i++) 
             { 
              if($i<=$hop_count) 
              { 
               $path[$i] = $hop[$i];       
               $lastIndex = $i; 
              } 
              else 
              { 
               $path[$i] = null; 
              } 
             } 
             $path[$lastIndex+1] = $router_name; 

             for($i=1; $i<=$maxPath; $i++) 
             { 
              if($path[$i] != NULL) 
              { 
               $hop_route[$i-1] = $path[$i-1].'>'.$path[$i]; 
               echo $hop_route[$i-1].'<br/>'; 
              } 
              else 
              { 
               $hop_route[$i-1] = NULL; 
              } 
             }      


             //inserting into database 
             $sql1 = "INSERT INTO hop (hop_id, hop_1_route, hop_2_route, hop_3_route, hop_4_route, hop_5_route, hop_6_route, hop_7_route, hop_8_route, hop_9_route, hop_count, site_name) VALUES ('', '".$hop_route[0]."', '".$hop_route[1]."', '".$hop_route[2]."', '".$hop_route[3]."', '".$hop_route[4]."', '".$hop_route[5]."', '".$hop_route[6]."', '".$hop_route[7]."', '".$hop_route[8]."', '".$hop_count."', '".$site_name."')"; 
             if ($conn->query($sql1) === TRUE) 
             { 
              //echo "New record created successfully"; 
             } 
             else 
             { 
              echo "Error: " . $sql1 . "<br>" . $conn->error; 
             }