Question

I have the following queries

$queryrs12c = "SELECT COUNT(*) total, SUM(goals) as sumgoals, SUM(assists) as sumassists, SUM(minutes) as summinutes FROM `player` WHERE season='12' AND gametype='club' ";

$queryrs11c = "SELECT COUNT(*) total, SUM(goals) as sumgoals, SUM(assists) as sumassists, SUM(minutes) as summinutes FROM `player` WHERE season='11' AND gametype='club' ";

$queryrs10c = "SELECT COUNT(*) total, SUM(goals) as sumgoals, SUM(assists) as sumassists, SUM(minutes) as summinutes FROM `player` WHERE season='10' AND gametype='club' ";

etc

I have this query 12 times cycling through 1-12

I then take the results of each query through a loop to generate 12 tables of data.

Is there way to combine all 12 of the queries but still retain the ability to generate 12 tables of data?

(12 queries doesn't seem a lot but I have other sets of similar queries, one with 10 queries, one with 12 and one with 13 and there are so many variables that I have to cycle through.)

Was it helpful?

Solution

You should be using a GROUP BY clause, which will allow you to use aggregate functions like SUM() for each variation in the column by which you are grouping. In your case, this might look like this:

SELECT
    season,
    COUNT(*) AS total,
    SUM(goals) AS sumgoals,
    SUM(assists) AS sumassists,
    SUM(minutes) AS summinutes
FROM `player`
WHERE gametype='club'
GROUP BY season
ORDER BY season DESC /* or ASC whichever you wish */

OTHER TIPS

SELECT 
    season,
    COUNT(*) total, 
    SUM(goals) as sumgoals, 
    SUM(assists) as sumassists, 
    SUM(minutes) as summinutes 
FROM `player` 
WHERE season between 1 and 12
AND gametype='club'
GROUP BY season
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top