Update row with data from another row in the same table

前端 未结 5 1372
一向
一向 2020-11-27 02:59

I\'ve got a table which looks something like this

ID   |   NAME    |  VALUE  |
----------------------------
 1   |   Test    |  VALUE1 |
 2   |   Test2   |          


        
相关标签:
5条回答
  • 2020-11-27 03:17

    Here's my go:

    UPDATE test as t1 
        INNER JOIN test as t2 ON 
            t1.NAME = t2.NAME AND 
            t2.value IS NOT NULL 
    SET t1.VALUE = t2.VALUE;
    

    EDIT: Removed superfluous t1.id != t2.id condition.

    0 讨论(0)
  • 2020-11-27 03:23
    UPDATE financialyear
       SET firstsemfrom = dt2.firstsemfrom,
           firstsemto = dt2.firstsemto,
           secondsemfrom = dt2.secondsemfrom,
           secondsemto = dt2.secondsemto
      from financialyear dt2
     WHERE financialyear.financialyearkey = 141
       AND dt2.financialyearkey = 140
    
    0 讨论(0)
  • 2020-11-27 03:31

    If you just need to insert a new row with a data from another row,

        insert into ORDER_ITEM select * from ORDER_ITEM where ITEM_NUMBER =123;
    
    0 讨论(0)
  • 2020-11-27 03:32

    Try this:

    UPDATE data_table t, (SELECT DISTINCT ID, NAME, VALUE
                            FROM data_table
                           WHERE VALUE IS NOT NULL AND VALUE != '') t1
       SET t.VALUE = t1.VALUE
     WHERE t.ID = t1.ID
       AND t.NAME = t1.NAME
    
    0 讨论(0)
  • 2020-11-27 03:38
    Update MyTable
    Set Value = (
                    Select Min( T2.Value )
                    From MyTable As T2
                    Where T2.Id <> MyTable.Id
                        And T2.Name = MyTable.Name
                    )
    Where ( Value Is Null Or Value = '' )
        And Exists  (
                    Select 1
                    From MyTable As T3
                    Where T3.Id <> MyTable.Id
                        And T3.Name = MyTable.Name
                    )
    
    0 讨论(0)
提交回复
热议问题