在一个查询中使用UNION的两个INSERT语句

时间:2016-08-04 14:07:38

标签: php mysql sql mysqli union

当我运行没有UNION的代码并使用一个SELECT时,它可以工作但是当我尝试一起运行它不会将数据插入表中,或者在没有UNION的情况下以不同的方式尝试,这里有一些帮助。

<?php
include("conf.php");
$query = "INSERT INTO fund(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());
while($row = mysqli_fetch_assoc($data)){
    foreach($row as $cname => $cvalue){
        print "$cname: $cvalue\t";
    }
    print "\r\n";
}
?>

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

INSERT INTO account(employees)
SELECT * FROM (
SELECT employees FROM account WHERE id = 1
UNION
SELECT employees FROM account WHERE id = 2
) AS a

将联合结果包装在另一个表中。

所以你的查询是:

    <?php
    include("conf.php");
    $query = "INSERT INTO fund(Team, GamesPlayedHome, GoalsScorredHome, GoalsAcceptedHome, RedCardGotHome, AvarageGoalsScorredHome, AvarageGoalsAcceptedHome, GamesPlayedAway, GoalsScorredAway, GoalsAcceptedAway, RedCardGotAway, AvarageGoalsScorredAway, AvarageGoalsAcceptedAway)
    seelct * 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
    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) as temp";
    $data= mysqli_query($conn,$query) or die(mysqli_error());
    while($row = mysqli_fetch_assoc($data)){
        foreach($row as $cname => $cvalue){
            print "$cname: $cvalue\t";
        }
        print "\r\n";
    }
    ?>

答案 1 :(得分:0)

使用UNION ALL时,两个SELECT查询中的列必须相同。在给定的查询中,列列表不匹配。

INSERT块中的13列但第一列SELECT包含7列,第二列SELECT包含6列。

因此,您可以为其余列添加NULL或零(对于非空列)。

希望您需要以下查询:

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
, NULL, NULL, NULL, NULL, NULL, NULL
FROM GetHistoricMatchesByLeagueAndDateInterval GROUP BY HomeTeam ORDER BY HomeTeam ASC)
UNION ALL
SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL,
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