嗨,有人可以告訴我如何將這兩個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();
?>
請一些幫助
末碼
<?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";
}
?>
從聯合做'INSERT INTO'是我該怎麼做的。錯誤消息非常明瞭:聯合中的兩個查詢不會返回相同數量的列。 –
母豬你有任何代碼消化 – user3255412
你最清楚丟失的列應該是什麼。 –