MySQL/UPDATE

来自康健生活
Kangkk​(留言 | 贡献)2022年9月14日 (三) 09:58的版本 (→引言)

引言

更新值。

UPDATE `md_history` SET `message` = null;

将搜索结果作为更新值。

update md_work w set folder_id = (
  select tmp.folder_id
  from (
    select team_id, id as folder_id from md_folder f
  ) tmp
  where w.team_id = tmp.team_id
);

复杂更新

UPDATE 
  md_team_user tu
INNER JOIN
  (
    SELECT
      MAX(tmp.create_at) AS create_at,
      tmp.team_id,
      tmp._user_id
    FROM 
      (
        SELECT
          thf.team_id AS team_id,
          thf.user_id AS user_id,
          thf.message -> '$.user' AS _user_id,
          thf.create_at AS create_at
        FROM
          md_team_history thf 
        WHERE
          thf.message -> '$.user' IS NOT NULL
      ) AS tmp
    GROUP BY
      tmp.team_id,
      tmp._user_id
    ORDER BY
      tmp.team_id,
      tmp._user_id
  ) as cup
ON
  tu.team_id = cup.team_id
  AND tu.user_id = cup._user_id
SET
  tu.create_at = cup.create_at;
COMMIT;