提问者:小点点

MySQL将两个表中的计算划分为新表中的更新


在我当前的项目中,我希望从表game和表game_stats中划分值​​​,并将其保存在表game_stats_more中作为更新。 我已经尝试在SQL更新中实现这一点,但不幸的是,在表game_stats_more中只输入了值1,而没有输入计算值。

这里的问题出在哪里?

UPDATE game_stats_more INNER JOIN game ON game_stats_more.game_id = game.match_id INNER JOIN game_stats ON game_stats_more.game_stats_id = game_stats.match_stats_id
SET game_stats_more_kpr = game_stats.match_stats_kills / (game.match_score_team_1 + game.match_score_team_2), 
game_stats_more_hsp = game_stats.match_stats_hs / game_stats.match_stats_kills, 
game_stats_more_kd = game_stats.match_stats_kills / game_stats.match_stats_deaths 
WHERE game_stats_id = 1 AND game_id = 1;
CREATE TABLE `game` (
  `match_id` int(3) NOT NULL,
  `team_id` int(3) NOT NULL,
  `match_team_2` varchar(255) COLLATE utf8_german2_ci NOT NULL,
  `match_score_team_1` int(2) NOT NULL,
  `match_score_team_2` int(2) NOT NULL,
  `match_score_role_id` int(3) NOT NULL,
  `match_role_id` int(3) NOT NULL,
  `date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_german2_ci;

INSERT INTO `game` (`match_id`, `team_id`, `match_team_2`, `match_score_team_1`, `match_score_team_2`, `match_score_role_id`, `match_role_id`, `date`) VALUES
(1, 7, 'TestGegner', 7, 2, 1, 1, '2020-06-10'),

CREATE TABLE `game_stats` (
  `match_stats_id` int(3) NOT NULL,
  `match_id` int(3) NOT NULL,
  `user_id` int(3) NOT NULL,
  `match_stats_kills` int(2) NOT NULL,
  `match_stats_deaths` int(2) NOT NULL,
  `match_stats_entry_kill` int(2) NOT NULL,
  `match_stats_entry_death` int(2) NOT NULL,
  `match_stats_clutch` int(2) NOT NULL,
  `match_stats_plants` int(2) NOT NULL,
  `match_stats_hs` int(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_german2_ci;

INSERT INTO `game_stats` (`match_stats_id`, `match_id`, `user_id`, `match_stats_kills`, `match_stats_deaths`, `match_stats_entry_kill`, `match_stats_entry_death`, `match_stats_clutch`, `match_stats_plants`, `match_stats_hs`) VALUES
(1, 1, 1, 7, 5, 1, 3, 1, 4, 4),
(2, 1, 2, 6, 6, 2, 2, 0, 1, 3);

CREATE TABLE `game_stats_more` (
  `game_stats_id` int(3) NOT NULL,
  `game_id` int(3) NOT NULL,
  `game_stats_more_kpr` int(3) NOT NULL,
  `game_stats_more_hsp` int(3) NOT NULL,
  `game_stats_more_kd` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_german2_ci;

INSERT INTO `game_stats_more` (`game_stats_id`, `game_id`, `game_stats_more_kpr`, `game_stats_more_hsp`, `game_stats_more_kd`) VALUES
(1, 1, 0, 0, 0);

共1个答案

匿名用户

您将XOLUMN定义为INT,它将始终使您五个四舍五入的数字

试试看

CREATE TABLE `game_stats_more` (
  `game_stats_id` int(3) NOT NULL,
  `game_id` int(3) NOT NULL,
  `game_stats_more_kpr` DECIMAL(3,3) NOT NULL,
  `game_stats_more_hsp` DECIMAL(3,3) NOT NULL,
  `game_stats_more_kd` DECIMAL(10,3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_german2_ci;

这将给出xyou查询#1

SELECT * FROM game_stats_more;

| game_stats_id | game_id | game_stats_more_kpr | game_stats_more_hsp | game_stats_more_kd |
| ------------- | ------- | ------------------- | ------------------- | ------------------ |
| 1             | 1       | 0.778               | 0.571               | 1.400              |

查询#2

SELECT
game_stats.match_stats_kills / (game.match_score_team_1 + game.match_score_team_2)
,game_stats.match_stats_hs / game_stats.match_stats_kills

,game_stats.match_stats_kills / game_stats.match_stats_deaths
FROM
game_stats_more
        INNER JOIN
    game ON game_stats_more.game_id = game.match_id
        INNER JOIN
    game_stats ON game_stats_more.game_stats_id = game_stats.match_stats_id;

| game_stats.match_stats_kills / (game.match_score_team_1 + game.match_score_team_2) | game_stats.match_stats_hs / game_stats.match_stats_kills | game_stats.match_stats_kills / game_stats.match_stats_deaths |
| ---------------------------------------------------------------------------------- | -------------------------------------------------------- | ------------------------------------------------------------ |
| 0.7778                                                                             | 0.5714                                                   | 1.4000                                                       |