【问题标题】:SQL query more time to execute and finally get error [closed]SQL查询更多时间执行并最终出错[关闭]
【发布时间】:2020-06-04 06:37:29
【问题描述】:
UPDATE member
SET member.usableSum = (SELECT usablesum
        FROM fundrecord
        WHERE (member.id= fundrecord.memberId) AND id IN (
        SELECT MAX(id)
        FROM [enter image description here][1]fundrecord
        GROUP BY memberId)
        )

【问题讨论】:

  • 你的子查询在这里只允许返回一行,但可能会返回几行。
  • 发布错误消息。
  • 还添加一些示例数据以及您的预期输出。

标签: mysql sql execution


【解决方案1】:

我猜你想要:

UPDATE member m
    SET m.usableSum = (SELECT fr.usablesum
                       FROM fundrecord fr
                       WHERE m.id = fr.memberId
                       ORDER BY fr.id DESC
                       LIMIT 1
                      );

这将为每个成员返回来自fundrecord 的“最新”usablesum

【讨论】:

    【解决方案2】:

    请在查询中使用DISTINCT

    UPDATE member
    SET member.usableSum = (SELECT distinct usablesum
        FROM fundrecord
        WHERE (member.id= fundrecord.memberId) AND id IN (
        SELECT MAX(id)
        FROM [enter image description here][1]fundrecord
        GROUP BY memberId)
        )
    

    如果您仍然面临同样的问题,那么您的子查询将返回多个值

    【讨论】:

      猜你喜欢
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多