vivis933 Posted August 4, 2016 Share Posted August 4, 2016 When i run the code without UNION and with one SELECT it works but when i try to run all together it wont insert data into table, or to try in different way without UNION, some help here. <?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"; } ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 4, 2016 Share Posted August 4, 2016 Probably s/b on the sql forum. It's not a php problem Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 4, 2016 Share Posted August 4, 2016 When using UNION, the queries MUST have the same number of fields and the same field types (in the same order). The two queries you are trying to UNION fail just from the first fields. The first field in the first query is a team name, but the first field in the second query is a count. And, from what I deduce you should not be using UNION. It is used to combine records from multiple queries where each record is independent. I think you mean to use a JOIN. But, it appears you are trying to calculate data and store in another table. That is a bad practice. Just get the calculated data from the source tables when you need the data. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 4, 2016 Share Posted August 4, 2016 (edited) But, it appears you are trying to calculate data and store in another table. That is a bad practice. Just get the calculated data from the source tables when you need the data. Something I've already told them in another post (http://forums.phpfreaks.com/topic/301723-error-saving-data-in-mysql-table/). Why do we bother? And why are you doing this at all? You can get these by query any time, you should not store data derived from the data in databases. Edited August 4, 2016 by Barand Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.