2016-08-09 20 views
0

私はこのコードを作成しましたが、それを実行すると最初の行だけがテーブルを作成しますが、テーブルには複数の行があります。 。いくつかの助けここMySql:クエリは選択から最初の行のみを返します

<?php 
include("conf.php"); 
$file_name = "testing2"; 
$table = "TEMP_".$file_name; 
$table1 = "TEMP1_".$file_name; 
$query = "CREATE TABLE IF NOT EXISTS $table1(Id INT AUTO_INCREMENT PRIMARY KEY) 
CHARACTER SET utf8 COLLATE utf8_general_ci 
SELECT Team, GamesPlayedHome, GoalsScorredHome, GoalsAcceptedHome, RedCardGotHome, AvarageGoalsScorredHome, AvarageGoalsAcceptedHome, GamesPlayedAway, GoalsScorredAway, GoalsAcceptedAway, RedCardGotAway, AvarageGoalsScorredAway, AvarageGoalsAcceptedAway, 
AvarageRedCardGotHome, AvarageRedCardGotAway, AvarageShotsOnTargedHome, AvarageShotsOnTargedAway, 
AvarageGoalsScorredHome/avg(AvarageGoalsScorredHome) AS AttackingStrengthHome, AvarageGoalsAcceptedHome/avg(AvarageGoalsAcceptedHome) AS DefensiveStrengthHome, 
AvarageGoalsScorredAway/avg(AvarageGoalsScorredAway) AS AttackingStrengthAway, AvarageGoalsAcceptedAway/avg(AvarageGoalsAcceptedAway) AS DefensiveStrengthAway 
FROM $table 
"; 
$data= mysqli_query($conn,$query) or die(mysqli_error($conn)); 
while($row = mysqli_fetch_assoc($data)){ 
    foreach($row as $cname => $cvalue){ 
     print "$cname: $cvalue\t"; 
    } 
    print "\r\n"; 
} 
?> 

答えて

2

あなたのクエリはありませんGROUP BYと集計関数(avg())を持っています。しかし、単にGROUP BYを追加するだけでは正しいこととは思われません。

SELECT Team, GamesPlayedHome, GoalsScorredHome, GoalsAcceptedHome, 
     RedCardGotHome, AvarageGoalsScorredHome, AvarageGoalsAcceptedHome, 
     GamesPlayedAway, GoalsScorredAway, GoalsAcceptedAway, RedCardGotAway, 
     AvarageGoalsScorredAway, AvarageGoalsAcceptedAway, 
     AvarageRedCardGotHome, AvarageRedCardGotAway, AvarageShotsOnTargedHome, AvarageShotsOnTargedAway, 
     AvarageGoalsScorredHome/tt.avg_AvarageGoalsScorredHome AS AttackingStrengthHome, 
     AvarageGoalsAcceptedHome/tt.avg_AvarageGoalsAcceptedHome AS DefensiveStrengthHome, 
     AvarageGoalsScorredAway/tt.avg_AvarageGoalsScorredAway AS AttackingStrengthAway, 
     AvarageGoalsAcceptedAway/tt.avg_AvarageGoalsAcceptedAway AS DefensiveStrengthAway 
FROM $table t CROSS JOIN 
    (SELECT avg(AvarageGoalsScorredHome) as avg_AvarageGoalsScorredHome, 
      avg(AvarageGoalsAcceptedHome) as avg_AvarageGoalsAcceptedHome, 
      avg(AvarageGoalsScorredAway) as avg_AvarageGoalsScorredAway, 
      avg(AvarageGoalsAcceptedAway) as avg_AvarageGoalsAcceptedAway 
     FROM $table tt 
    ) tt; 

注:私はあなたがこのような何かをしたいと思うあなたは正しく、列名を綴る必要があります。「平均」とは、正しいスペルである「得点しました」。

関連する問題