0

I have a table for game stats. How do I add all the player's points from 18 different tables. I'm trying this but I get nothing, blank page.

 $result = mysqli_query($conn, "SELECT (SELECT kp.pts + kp2.pts2 + kp3.pts3 + kp4.pts4 + kp5.pts5 + kp6.pts6 + kp7.pts7 + kp8.pts8 + kp9.pts9  AS sum1 
FROM knightsplayer AS kp
JOIN knightsplayer2 AS kp2 ON kp.id = kp2.id2
JOIN knightsplayer3 AS kp3 ON kp.id = kp3.id3 
JOIN knightsplayer4 AS kp4 ON kp.id = kp4.id4 
JOIN knightsplayer5 AS kp5 ON kp.id = kp5.id5 
JOIN knightsplayer6 AS kp6 ON kp.id = kp6.id6 
JOIN knightsplayer7 AS kp7 ON kp.id = kp7.id7 
JOIN knightsplayer8 AS kp8 ON kp.id = kp8.id8 
JOIN knightsplayer9 AS kp9 ON kp.id = kp9.id9);

(SELECT bp.pts + bp2.pts2 + bp3.pts3 + bp4.pts4 + bp5.pts5 + bp6.pts6 + bp7.pts7 + bp8.pts8 + bp9.pts9  AS sum2 
FROM blazersplayer AS bp
JOIN blazersplayer2 AS bp2 ON bp.id = bp2.id2
JOIN blazersplayer3 AS bp3 ON bp.id = bp3.id3 
JOIN blazersplayer4 AS bp4 ON bp.id = bp4.id4 
JOIN blazersplayer5 AS bp5 ON bp.id = bp5.id5 
JOIN blazersplayer6 AS bp6 ON bp.id = bp6.id6 
JOIN blazersplayer7 AS bp7 ON bp.id = bp7.id7 
JOIN blazersplayer8 AS bp8 ON bp.id = bp8.id8 
JOIN blazersplayer9 AS bp9 ON bp.id = bp9.id9)");

Each table is for one game, so knightsplayer is the stats for the first game for the team "Knights". knightsplayer2 is the stats for the second game and so on. I have to add all the points of the individual players from both teams and display them as one table. Basically a leader board for points.

3
  • 3
    Fix your data structure so you store all the game results in a single table. Commented Feb 22, 2017 at 2:52
  • Why in the world would you use a separate table for each game? Put them all in one table with a column for GAMENO or GAMENUMBER, and then you can actually use the data. What you have now is totally useless. What happens when there is a 17th (or 18th, or 32nd) game played later? Commented Feb 22, 2017 at 2:59
  • Actually, to normalize the data, you would probably need a Player table, Game table, and Player/Game/Score table. Commented Feb 22, 2017 at 3:13

2 Answers 2

1

I suggest you to migrate data to the next strcuture: enter image description here

-- ----------------------------
-- Table structure for game
-- ----------------------------
DROP TABLE IF EXISTS `game`;
CREATE TABLE `game` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) DEFAULT NULL,
  `active` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of game
-- ----------------------------
INSERT INTO `game` VALUES ('1', 'knights', '1');
INSERT INTO `game` VALUES ('2', 'blazers', '1');

-- ----------------------------
-- Table structure for player
-- ----------------------------
DROP TABLE IF EXISTS `player`;
CREATE TABLE `player` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) DEFAULT NULL,
  `active` tinyint(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of player
-- ----------------------------
INSERT INTO `player` VALUES ('1', 'player1', '1');
INSERT INTO `player` VALUES ('2', 'player2', '1');
INSERT INTO `player` VALUES ('3', 'player3', '1');
INSERT INTO `player` VALUES ('4', 'player4', '1');
INSERT INTO `player` VALUES ('5', 'player5', '1');
INSERT INTO `player` VALUES ('6', 'player6', '1');
INSERT INTO `player` VALUES ('7', 'player7', '1');
INSERT INTO `player` VALUES ('8', 'player8', '1');
INSERT INTO `player` VALUES ('9', 'player9', '1');

-- ----------------------------
-- Table structure for statistics
-- ----------------------------
DROP TABLE IF EXISTS `statistics`;
CREATE TABLE `statistics` (
  `game_id` int(11) DEFAULT NULL,
  `player_id` int(11) DEFAULT NULL,
  `points` decimal(10,2) DEFAULT NULL,
  KEY `player_fk` (`player_id`),
  KEY `game_fk` (`game_id`),
  CONSTRAINT `game_fk` FOREIGN KEY (`game_id`) REFERENCES `game` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION,
  CONSTRAINT `player_fk` FOREIGN KEY (`player_id`) REFERENCES `player` (`id`) ON DELETE SET NULL ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of statistics
-- ----------------------------
INSERT INTO `statistics` VALUES ('1', '3', '23.00');
INSERT INTO `statistics` VALUES ('1', '1', '345.00');
INSERT INTO `statistics` VALUES ('1', '4', '4.00');
INSERT INTO `statistics` VALUES ('1', '2', '345.00');
INSERT INTO `statistics` VALUES ('1', '5', '23.00');
INSERT INTO `statistics` VALUES ('1', '6', '445.00');
INSERT INTO `statistics` VALUES ('1', '7', '47.00');
INSERT INTO `statistics` VALUES ('1', '8', '5.00');
INSERT INTO `statistics` VALUES ('1', '9', '523.00');
INSERT INTO `statistics` VALUES ('2', '1', '2341.00');
INSERT INTO `statistics` VALUES ('2', '2', '0.00');
INSERT INTO `statistics` VALUES ('2', '3', '56.00');
INSERT INTO `statistics` VALUES ('2', '4', '756.00');
INSERT INTO `statistics` VALUES ('2', '5', '755.00');
INSERT INTO `statistics` VALUES ('2', '6', '56.00');
INSERT INTO `statistics` VALUES ('2', '7', '345.00');
INSERT INTO `statistics` VALUES ('2', '8', '523.00');
INSERT INTO `statistics` VALUES ('2', '9', '12.00');

Then your can query:

SELECT
game.`name`,
SUM(statistics.points)
FROM
statistics
INNER JOIN game ON game.id = statistics.game_id
GROUP BY game.`id`
Sign up to request clarification or add additional context in comments.

Comments

0

You have a subquery in your SELECT clause, but you don't have a FROM clause to complete the outer query.

Also, the ; between the two subqueries, denotes the end of the SQL statement.

You can do something like:

SELECT sum1, sum2
FROM (subquery1 here) as table1,
     (subquery2 here) as table2;

As in (using your example):

"SELECT sum1, sum2
 FROM (SELECT kp.pts + kp2.pts2 + kp3.pts3 + kp4.pts4 + kp5.pts5 + kp6.pts6 + kp7.pts7 + kp8.pts8 + kp9.pts9  AS sum1 
       FROM knightsplayer AS kp
       JOIN knightsplayer2 AS kp2 ON kp.id = kp2.id2
       JOIN knightsplayer3 AS kp3 ON kp.id = kp3.id3 
       JOIN knightsplayer4 AS kp4 ON kp.id = kp4.id4 
       JOIN knightsplayer5 AS kp5 ON kp.id = kp5.id5 
       JOIN knightsplayer6 AS kp6 ON kp.id = kp6.id6 
       JOIN knightsplayer7 AS kp7 ON kp.id = kp7.id7 
       JOIN knightsplayer8 AS kp8 ON kp.id = kp8.id8 
       JOIN knightsplayer9 AS kp9 ON kp.id = kp9.id9) as table1,
      (SELECT bp.pts + bp2.pts2 + bp3.pts3 + bp4.pts4 + bp5.pts5 + bp6.pts6 + bp7.pts7 + bp8.pts8 + bp9.pts9  AS sum2 
       FROM blazersplayer AS bp
       JOIN blazersplayer2 AS bp2 ON bp.id = bp2.id2
       JOIN blazersplayer3 AS bp3 ON bp.id = bp3.id3 
       JOIN blazersplayer4 AS bp4 ON bp.id = bp4.id4 
       JOIN blazersplayer5 AS bp5 ON bp.id = bp5.id5 
       JOIN blazersplayer6 AS bp6 ON bp.id = bp6.id6 
       JOIN blazersplayer7 AS bp7 ON bp.id = bp7.id7 
       JOIN blazersplayer8 AS bp8 ON bp.id = bp8.id8 
       JOIN blazersplayer9 AS bp9 ON bp.id = bp9.id9) as table2;"

Or, if you need to join the two inline views:

SELECT sum1, sum2
FROM (subquery1 here) as table1 
JOIN (subquery2 here) as table2
ON (join condition); 

Keep in mind that if you do need to join the two inline views, you need to select a column (aside from sum1/sum2) to join on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.