MySQL - UPDATE query based on SELECT Query

后端 未结 11 2083
既然无缘
既然无缘 2020-11-21 23:58

I need to check (from the same table) if there is an association between two events based on date-time.

One set of data will contain the ending date-time of certain

11条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 00:51

    I had an issue with duplicate entries in one table itself. Below is the approaches were working for me. It has also been advocated by @sibaz.

    Finally I solved it using the below queries:

    1. The select query is saved in a temp table

      IF OBJECT_ID(N'tempdb..#New_format_donor_temp', N'U') IS NOT NULL
          DROP TABLE #New_format_donor_temp;
      
      select *
      into #New_format_donor_temp
      from DONOR_EMPLOYMENTS
      where DONOR_ID IN (
        1, 2
      )
      
      -- Test New_format_donor_temp
      -- SELECT *
      -- FROM #New_format_donor_temp;
      
    2. The temp table is joined in the update query.

      UPDATE de
      SET STATUS_CD=de_new.STATUS_CD, STATUS_REASON_CD=de_new.STATUS_REASON_CD, TYPE_CD=de_new.TYPE_CD
      FROM DONOR_EMPLOYMENTS AS de
        INNER JOIN #New_format_donor_temp AS de_new ON de_new.EMP_NO = de.EMP_NO
      WHERE
        de.DONOR_ID IN (
          3, 4
      )
      

    I not very experienced with SQL please advise any better approach you know.

    Above queries are for MySql server.

提交回复
热议问题