解决MySQL Update:You can't specify target table for update in FROM clause

In MySQL You can not specify target table for update in FROM clause error mean, you can not select a certain value to the same table, and then update the table (in the same statement). For example the following sql:

Update  
    tms_jyxx_mx 
the SET  
    gg_dm =  ' 1004 '  
the WHERE  
    jyxx_id 
in  
    ( the SELECT 
        t1.jyxx_id AS jyxx_id
     the FROM 
        tms_jyxx T1, 
        tms_jyxx_mx T2 
    the WHERE 
        t1.sjmc =  ' Baoding Baigou Metro stations Lei Ming ' 
    the AND t1.jyxx_id = t2.jyxx_id
     the AND T2. ljqh =  ' 14 ' 
    the AND t2.jysj > =  ' 2019-04-30 10:17:22 ' 
    the AND t2.jysj<= '2019-09-26 23:59:59'
    ORDER BY
        t2.jysj
    desc)

On the line into the following:

Update  
    tms_jyxx_mx 
the SET  
    gg_dm =  ' 1004 '  
the WHERE  
    jyxx_id 
in  
    ( the SELECT 
        tt.jyxx_id 
    from 
    ( the SELECT 
        t1.jyxx_id AS jyxx_id
     the FROM 
        tms_jyxx T1, 
        tms_jyxx_mx T2 
    the WHERE 
        t1.sjmc =  ' Baoding Baigou Metro stations Lei Ming ' 
    the AND t1.jyxx_id = t2.jyxx_id
     the AND t2.ljqh =  ' 14 ' 
    the AND t2.jysj > =  '2019-04-30 10:17:22'
    AND t2.jysj <= '2019-09-26 23:59:59'
    ORDER BY
        t2.jysj
    desc) tt)

That will select the results and then select it again by the middle of the table, thus avoiding the error. Note that this problem occurs only in MySQL, MsSql and Oracle does not exhibit this problem.

Guess you like

Origin www.cnblogs.com/chuanqi1995/p/11589481.html