如何从多个表中添加数据

时间:2017-02-22 02:51:15

标签: php mysql sql

我有一张游戏统计数据表。如何从18个不同的表中添加所有玩家的积分。我正在尝试这个,但我什么也没得到,空白页。

 $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)");

每张桌子都适用于一款游戏,所以knightsplayer是团队“骑士”第一款游戏的统计数据。 knightsplayer2是第二场比赛的统计数据,依此类推。我必须添加来自两支球队的各个球员的所有积分并将其显示为一张桌子。基本上是积分榜的领导者。

2 个答案:

答案 0 :(得分:1)

我建议您将数据迁移到下一个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');

然后你可以查询:

SELECT
game.`name`,
SUM(statistics.points)
FROM
statistics
INNER JOIN game ON game.id = statistics.game_id
GROUP BY game.`id`

答案 1 :(得分:0)

您的self.addChildViewController(vc) vc.view.frame = CGRect(x: 0, y: 0, width: self.container.frame.size.width, height: self.container.frame.size.height) self.container.addSubview(vc.view) vc.didMoveToParentViewController(self) 子句中有子查询,但您没有SELECT子句来完成外部查询。

此外,两个子查询之间的FROM表示SQL语句的结束。

您可以执行以下操作:

;

如(使用你的例子):

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

或者,如果您需要加入两个内联视图:

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

请注意,如果您确实需要加入两个内联视图,则需要选择一个列(除了sum1 / sum2)才能加入。