MySQL - UPDATE query based on SELECT Query

后端 未结 11 2073
既然无缘
既然无缘 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:32

    For same table,

    UPDATE PHA_BILL_SEGMENT AS PHA,
         (SELECT BILL_ID, COUNT(REGISTRATION_NUMBER) AS REG 
           FROM PHA_BILL_SEGMENT
            GROUP BY REGISTRATION_NUMBER, BILL_DATE, BILL_AMOUNT
            HAVING REG > 1) T
        SET PHA.BILL_DATE = PHA.BILL_DATE + 2
     WHERE PHA.BILL_ID = T.BILL_ID;
    
    0 讨论(0)
  • 2020-11-22 00:44

    If somebody is seeking to update data from one database to another no matter which table they are targeting, there must be some criteria to do it.

    This one is better and clean for all levels:

    UPDATE dbname1.content targetTable
    
    LEFT JOIN dbname2.someothertable sourceTable ON
        targetTable.compare_field= sourceTable.compare_field
    SET
        targetTable.col1  = sourceTable.cola,
        targetTable.col2 = sourceTable.colb, 
        targetTable.col3 = sourceTable.colc, 
        targetTable.col4 = sourceTable.cold 
    

    Traaa! It works great!

    With the above understanding, you can modify the set fields and "on" criteria to do your work. You can also perform the checks, then pull the data into the temp table(s) and then run the update using the above syntax replacing your table and column names.

    Hope it works, if not let me know. I will write an exact query for you.

    0 讨论(0)
  • 2020-11-22 00:50

    You can actually do this one of two ways:

    MySQL update join syntax:

    UPDATE tableA a
    INNER JOIN tableB b ON a.name_a = b.name_b
    SET validation_check = if(start_dts > end_dts, 'VALID', '')
    -- where clause can go here
    

    ANSI SQL syntax:

    UPDATE tableA SET validation_check = 
        (SELECT if(start_DTS > end_DTS, 'VALID', '') AS validation_check
            FROM tableA
            INNER JOIN tableB ON name_A = name_B
            WHERE id_A = tableA.id_A)
    

    Pick whichever one seems most natural to you.

    0 讨论(0)
  • 2020-11-22 00:51

    Easy in MySQL:

    UPDATE users AS U1, users AS U2 
    SET U1.name_one = U2.name_colX
    WHERE U2.user_id = U1.user_id
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 00:53
    UPDATE
        `table1` AS `dest`,
        (
            SELECT
                *
            FROM
                `table2`
            WHERE
                `id` = x
        ) AS `src`
    SET
        `dest`.`col1` = `src`.`col1`
    WHERE
        `dest`.`id` = x
    ;
    

    Hope this works for you.

    0 讨论(0)
提交回复
热议问题