2016-08-05 77 views
1

嗨,有人可以告訴我如何將這兩個select語句插入到一個表中。插入兩個SELECT語句到mysql表中

INSERT INTO test(Team, GamesPlayedHome, GoalsScorredHome, GoalsAcceptedHome, RedCardGotHome, AvarageGoalsScorredHome, AvarageGoalsAcceptedHome, GamesPlayedAway, GoalsScorredAway, GoalsAcceptedAway, RedCardGotAway, AvarageGoalsScorredAway, AvarageGoalsAcceptedAway) 

SELECT HomeTeam As Team, COUNT(HomeTeam) AS GamesPlayedHome, SUM(HomeGoals) AS GoalsScorredHome, SUM(AwayGoals) AS GoalsAcceptedHome, SUM(HomeRedCards) AS RedCardGotHome, 
SUM(HomeGoals)/COUNT(HomeTeam) AS AvarageGoalsScorredHome, SUM(AwayGoals)/COUNT(HomeTeam) AS AvarageGoalsAcceptedHome 
FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY HomeTeam ORDER BY HomeTeam ASC 

SELECT COUNT(AwayTeam) AS GamesPlayedAway, SUM(AwayGoals) AS GoalsScorredAway, SUM(HomeGoals) AS GoalsAcceptedAway, SUM(AwayRedCards) AS RedCardGotAway, 
SUM(HomeGoals)/COUNT(AwayTeam) AS AvarageGoalsScorredAway, SUM(AwayGoals)/COUNT(AwayTeam) AS AvarageGoalsAcceptedAway 
FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY AwayTeam ORDER BY AwayTeam ASC 

我已經tryed這

<?php 
include("conf.php"); 
$query = "INSERT INTO test(Team, GamesPlayedHome, GoalsScorredHome, GoalsAcceptedHome, RedCardGotHome, AvarageGoalsScorredHome, AvarageGoalsAcceptedHome, GamesPlayedAway, GoalsScorredAway, GoalsAcceptedAway, RedCardGotAway, AvarageGoalsScorredAway, AvarageGoalsAcceptedAway) 
(SELECT HomeTeam As Team, COUNT(HomeTeam) AS GamesPlayedHome, SUM(HomeGoals) AS GoalsScorredHome, SUM(AwayGoals) AS GoalsAcceptedHome, SUM(HomeRedCards) AS RedCardGotHome, 
SUM(HomeGoals)/COUNT(HomeTeam) AS AvarageGoalsScorredHome, SUM(AwayGoals)/COUNT(HomeTeam) AS AvarageGoalsAcceptedHome 
FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY HomeTeam ORDER BY HomeTeam ASC) 
UNION ALL 
(SELECT COUNT(AwayTeam) AS GamesPlayedAway, SUM(AwayGoals) AS GoalsScorredAway, SUM(HomeGoals) AS GoalsAcceptedAway, SUM(AwayRedCards) AS RedCardGotAway, 
SUM(HomeGoals)/COUNT(AwayTeam) AS AvarageGoalsScorredAway, SUM(AwayGoals)/COUNT(AwayTeam) AS AvarageGoalsAcceptedAway 
FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY AwayTeam ORDER BY AwayTeam ASC) 
"; 
$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"; 
} 
?> 

我得到這個錯誤,當我運行它

列數第1行

我不匹配值計數也試過用多查詢

<?php 
include("conf.php"); 
//QUERY 1 
$query = "INSERT INTO test(Team, GamesPlayedHome, GoalsScorredHome, GoalsAcceptedHome, RedCardGotHome, AvarageGoalsScorredHome, AvarageGoalsAcceptedHome) 
SELECT HomeTeam As Team, COUNT(HomeTeam) AS GamesPlayedHome, SUM(HomeGoals) AS GoalsScorredHome, SUM(AwayGoals) AS GoalsAcceptedHome, SUM(HomeRedCards) AS RedCardGotHome, 
SUM(HomeGoals)/COUNT(HomeTeam) AS AvarageGoalsScorredHome, SUM(AwayGoals)/COUNT(HomeTeam) AS AvarageGoalsAcceptedHome 
FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY HomeTeam ORDER BY HomeTeam ASC;"; 
//QUERY 2 
$query .= "INSERT INTO test(GamesPlayedAway, GoalsScorredAway, GoalsAcceptedAway, RedCardGotAway, AvarageGoalsScorredAway, AvarageGoalsAcceptedAway) 
SELECT COUNT(AwayTeam) AS GamesPlayedAway, SUM(AwayGoals) AS GoalsScorredAway, SUM(HomeGoals) AS GoalsAcceptedAway, SUM(AwayRedCards) AS RedCardGotAway, 
SUM(HomeGoals)/COUNT(AwayTeam) AS AvarageGoalsScorredAway, SUM(AwayGoals)/COUNT(AwayTeam) AS AvarageGoalsAcceptedAway 
FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY AwayTeam ORDER BY AwayTeam ASC"; 

/* execute multi query */ 
if ($conn->multi_query($query)) { 
    do { 
     /* store first result set */ 
     if ($result = $conn->store_result()) { 
      while ($row = $result->fetch_row()) { 
       printf("%s\n", $row[0]); 
      } 
      $result->free(); 
     } 
     /* print divider */ 
     if ($conn->more_results()) { 
      printf("-----------------\n"); 
     } 
    } while ($conn->next_result()); 
} 

/* close connection */ 
$conn->close(); 
?> 

當我運行這段代碼把它插入數據,但第二個查詢還插入空行enter image description here

請一些幫助

末碼

<?php 
include("conf.php"); 
$query = "SELECT querya.Team, GamesPlayedHome, GoalsScorredHome, GoalsAcceptedHome, RedCardGotHome, AvarageGoalsScorredHome, AvarageGoalsAcceptedHome, GamesPlayedAway, GoalsScorredAway, GoalsAcceptedAway, RedCardGotAway, AvarageGoalsScorredAway, AvarageGoalsAcceptedAway 
from 
(SELECT HomeTeam As Team, COUNT(HomeTeam) AS GamesPlayedHome, SUM(HomeGoals) AS GoalsScorredHome, SUM(AwayGoals) AS GoalsAcceptedHome, SUM(HomeRedCards) AS RedCardGotHome, 
SUM(HomeGoals)/COUNT(HomeTeam) AS AvarageGoalsScorredHome, SUM(AwayGoals)/COUNT(HomeTeam) AS AvarageGoalsAcceptedHome 
FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY HomeTeam ORDER BY HomeTeam ASC) 
querya inner join 
(SELECT AwayTeam As Team, COUNT(AwayTeam) AS GamesPlayedAway, SUM(AwayGoals) AS GoalsScorredAway, SUM(HomeGoals) AS GoalsAcceptedAway, SUM(AwayRedCards) AS RedCardGotAway, 
SUM(HomeGoals)/COUNT(AwayTeam) AS AvarageGoalsScorredAway, SUM(AwayGoals)/COUNT(AwayTeam) AS AvarageGoalsAcceptedAway 
FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY AwayTeam ORDER BY AwayTeam ASC) 
queryb on querya.Team = queryb.Team"; 
$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"; 
} 
?> 
+1

從聯合做'INSERT INTO'是我該怎麼做的。錯誤消息非常明瞭:聯合中的兩個查詢不會返回相同數量的列。 –

+0

母豬你有任何代碼消化 – user3255412

+0

你最清楚丟失的列應該是什麼。 –

回答

1

這是僞代碼在最好的,但它是一個嘗試。

Select (put all fields in here) 
from (put first query in its entirey here) querya 
inner join (put second query here, you will need to modify so it has a 'team' column) queryb 
on querya.team = queryb.team 

想一想嗎?聯盟是你正在嘗試的錯誤術語...加入是你在找什麼。

您有:不需要

"SELECT (Team, GamesPlayedHome, GoalsScorredHome, GoalsAcceptedHome, RedCardGotHome, AvarageGoalsScorredHome, AvarageGoalsAcceptedHome, GamesPlayedAway, GoalsScorredAway, GoalsAcceptedAway, RedCardGotAway, AvarageGoalsScorredAway, AvarageGoalsAcceptedAway) 
from ... 

括號。

"SELECT Team, GamesPlayedHome, GoalsScorredHome, GoalsAcceptedHome, RedCardGotHome, AvarageGoalsScorredHome, AvarageGoalsAcceptedHome, GamesPlayedAway, GoalsScorredAway, GoalsAcceptedAway, RedCardGotAway, AvarageGoalsScorredAway, AvarageGoalsAcceptedAway 
from ... 

我編輯了你的問題,從那裏使用它。

+0

我試過了,我得到這個錯誤'列'隊'在字段列表中是不明確的'我在代碼末尾添加代碼 – user3255412

+0

@ user3255412 - 您將需要指定哪個子查詢要從您的頂部取值選擇語句。 select select團隊的instea,請使用select querya.team。您可能必須使用多個列來完成此操作。 – Twelfth

+0

你能寫一個樣品嗎 – user3255412

0

這看起來像它應該'工作'(如在,沒有錯誤),但它可能不是你想要的和/或我可能被誤認爲...

INSERT INTO test 
(Team 
, GamesPlayedHome 
, GoalsScorredHome 
, GoalsAcceptedHome 
, RedCardGotHome 
, AvarageGoalsScorredHome 
, AvarageGoalsAcceptedHome 
, GamesPlayedAway 
, GoalsScorredAway 
, GoalsAcceptedAway 
, RedCardGotAway 
, AvarageGoalsScorredAway 
, AvarageGoalsAcceptedAway 
) 
SELECT HomeTeam 
    , COUNT(HomeTeam) 
    , SUM(HomeGoals) 
    , SUM(AwayGoals) 
    , SUM(HomeRedCards) 
    , SUM(HomeGoals)/COUNT(HomeTeam) 
    , SUM(AwayGoals)/COUNT(HomeTeam) 
    FROM GetHistoricMatchesByLeagueAndDateInterval 
GROUP 
    BY HomeTeam 
UNION 
    ALL 
SELECT COUNT(AwayTeam) 
    , SUM(AwayGoals) 
    , SUM(HomeGoals) 
    , SUM(AwayRedCards) 
    , SUM(HomeGoals)/COUNT(AwayTeam) 
    , SUM(AwayGoals)/COUNT(AwayTeam) 
    FROM GetHistoricMatchesByLeagueAndDateInterval 
GROUP 
    BY AwayTeam 

(你的拼寫是殘暴的,BTW)

+0

我得到了這個錯誤'使用的SELECT語句有不同數量的列' – user3255412

+0

這是行不通的。你只選擇了6列,但試圖插入12.但找到缺失列的榮譽,然而OP應該自己做。 –