【问题标题】:Operand should contain 1 column when joining tables with update使用更新连接表时,操作数应包含 1 列
【发布时间】:2013-12-08 12:58:55
【问题描述】:

我正在尝试通过组合两个查询来更新表,如下所示

 UPDATE result, games_played
      SET result.precentage_correct =
(
SELECT user_id, 100*SUM(n_win)/SUM(n_total) AS pct_win FROM
(SELECT user_id, COUNT(user_id) AS n_win,
  NULL AS n_total
  FROM games_played
  WHERE winner != 'n'
  AND game = 1
  GROUP BY user_id
  UNION SELECT user_id, NULL AS n_win,
  COUNT(user_id) AS n_total
  FROM games_played
  WHERE game = 1
  GROUP BY user_id
) AS counts
GROUP BY counts.user_id
)
WHERE result.user_id = games_played.user_id

但是我得到了错误

Operand should contain 1 column(s)

有谁知道我做错了什么...我可以选择结果作为新表

SQL 小提琴http://sqlfiddle.com/#!2/5374e6/1

【问题讨论】:

  • 不要选择user_id,只选择pct_win。错误消息意味着您尝试使用 2 个值/列更新一列(percentage_correct)
  • 谢谢,我现在明白为什么会出现此错误,但是我尝试了此操作,但出现错误子查询返回超过 1 行
  • 检查我对那个错误的回答

标签: mysql mysql-error-1241


【解决方案1】:

我不太确定您的查询的逻辑,但您可以通过在源表列表中将查询实际用作表来修复语法错误。

http://sqlfiddle.com/#!2/436eb/1

UPDATE result, games_played,
  (SELECT t1.user_id, 100*SUM(t1.n_win)/SUM(t1.n_total) AS pct_win FROM
   (SELECT user_id, COUNT(user_id) AS n_win, NULL AS n_total
     FROM games_played
     WHERE winner != 'n' AND game = 1
     GROUP BY user_id
    UNION 
    SELECT user_id, NULL AS n_win, COUNT(user_id) AS n_total
     FROM games_played
     WHERE game = 1
     GROUP BY user_id
   ) AS t1
  ) AS counts

SET result.precentage_correct = counts.pct_win
WHERE result.user_id = games_played.user_id

【讨论】:

  • 谢谢,但这实际上不起作用,它会更新所有用户的结果,值为 63...
  • 那将是另一个问题。你有一个语法问题。现在看来,您的查询在逻辑上也存在缺陷。
【解决方案2】:

您可以简化UPDATE。见SQL-Fiddle

UPDATE result
SET result.precentage_correct =
    ( SELECT 100*SUM(winner <> 'n')/COUNT(*) AS pct_win
      FROM games_played
      WHERE game = 1
        AND result.user_id = games_played.user_id
    ) ;

或者使用更像您的逻辑的查询。这甚至更好,因为(与前一个不同)它不会 UPDATE 整个 result 表(对于那些没有玩过游戏的用户为 Null),但只有那些在 games_played 表中至少有一个结果的用户.测试于SQL-Fiddle-2

UPDATE result AS r
  JOIN
    ( SELECT user_id, 
             100*SUM(winner <> 'n')/COUNT(*) AS pct_win
      FROM games_played
      WHERE game = 1
      GROUP BY user_id
    ) AS gp
    ON r.user_id = gp.user_id
SET r.precentage_correct = gp.pct_win ;

【讨论】:

    【解决方案3】:

    尝试在您的子查询中添加过滤条件。正如您在评论中提到的错误“子查询返回超过 1 行”意味着子查询(您命名为 AS 计数的那个)返回了多个结果。像这样的:

    UPDATE result, games_played
          SET result.precentage_correct =
    (
    SELECT 100*SUM(n_win)/SUM(n_total) AS pct_win FROM
    (SELECT user_id, COUNT(user_id) AS n_win,
      NULL AS n_total
      FROM games_played
      WHERE winner != 'n'
      AND game = 1
      GROUP BY user_id
      UNION SELECT user_id, NULL AS n_win,
      COUNT(user_id) AS n_total
      FROM games_played
      WHERE game = 1
      GROUP BY user_id
    ) AS counts
    GROUP BY counts.user_id
    HAVING counts.user_id = result.user_id
    )
    WHERE result.user_id = games_played.user_id
    

    【讨论】:

    • 感谢您提供答案并说明我出了什么问题:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多