how to choose which row to insert with same id in sql?

后端 未结 4 1641
深忆病人
深忆病人 2021-01-26 21:07

so Basically I have a table called \"table_1\" :

ID   Index          STATUS          TIME        DESCRIPTION
1     15          pending           1:00       Start         


        
4条回答
  •  故里飘歌
    2021-01-26 21:13

    INSERT INTO t2 (ID, STATUS, TIME)
    SELECT ID, STATUS, MIN(TIME) FROM t1 t1top
    WHERE EXISTS(SELECT * FROM t1 WHERE ID=t1top.ID AND STATUS='Complete')
    GROUP BY ID, STATUS
    ORDER BY CAST(ID AS UNSIGNED) ASC, STATUS DESC
    

    After the insert is made, if you want to see the result according to your example, you have to run the following select:

    SELECT ID, STATUS, TIME FROM table_1
    ORDER BY CAST(ID AS UNSIGNED) ASC, STATUS DESC
    

    thats EXACTLY RIGHT, however I dont want to just SEE it that way, it needs to be inserted in the second table in that manner as well, any idea?

提交回复
热议问题