gpt4 book ai didi

php - 如何从多个表添加数据

转载 作者:行者123 更新时间:2023-11-29 11:00:26 24 4
gpt4 key购买 nike

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

 $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 是“Knights”球队第一场比赛的统计数据。 Knightsplayer2 是第二场比赛的统计数据,依此类推。我必须将两支球队的各个球员的所有分数相加,并将它们显示为一张表格。基本上是积分排行榜。

最佳答案

我建议您将数据迁移到下一个结构: 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`

关于php - 如何从多个表添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42381805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com