Jump to content

Recommended Posts

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";
}
?>

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.

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 by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.