MySQL 视图 - 从多个表返回 NULL 的计算列

MySQL view - calculated column from multiple tables returning NULL

假设我有两个表。 Table T1 喜欢:

Game Player Points Assists
 1     1     10     5
 1     2     5      10

T2 赞:

Game Player Fouls Turnovers
1      1     5       5
1      2     10      10

我想创建一个视图,每个玩家一行,一个新字段 rating,其中 rating 是每个玩家 Points, Assists, Fouls,Turnovers 的等权重总和。 (即评分 = .25 * 得分 + .25 * 助攻 + .25 * 犯规 + .25 * 失误)

我创建视图:

CREATE VIEW `player_view` AS (
SELECT Player,
  SUM( 
  Points *0.25 +
  Assists *0.25 +
  Fouls *0.25 +
  Turnovers *0.25) AS Rating)
FROM T1 INNER JOIN T2 ON
  T1.Player = T2.Player
AND T1.Game = T2.Game
GROUP BY Player

但是我没有返回值,而是为所有 Rating:

得到 NULL
Player Rating
1       NULL
2       NULL

最初,我面对的是

Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'support_desk.mod_users_groups.group_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

所以我通过 SET sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

禁用了 only_full_group_by

所以现在,虽然查看returns一个结果集,Ratings都是NULL。请协助。

您似乎漏掉了求和列之间的运算符,例如 + 可能是你在关键字段中有一些隐藏的字符所以..试试 trim()

CREATE VIEW `player_view` AS 
SELECT Player,
  SUM( 
  t1.Points*0.25 + 
  t1.Assists*0.25  +
  t2.Fouls*0.25 + 
  t2.Turnovers*0.25) AS Rating
  )
FROM T1 
INNER JOIN T2 ON
  trim(T1.Player) = trim(T2.Player)
    AND trim(T1.Game) = trim(T2.Game);
GROUP BY Player

select * from player_view;