1. 程式人生 > >mysql 如何去重

mysql 如何去重

資料表:

sql程式碼:


delete  FROM  test where id NOT in(  select id FROM tmp  )


create table tmp (select id FROM test GROUP BY `name` )

tmp是建立的臨時表,不需要提前建立,直接寫接好,

為什麼要建立臨時表,看下面:

直接寫會報錯 :  

delete  FROM  test where id NOT in(  select id FROM test GROUP BY `name`  )

[SQL]

delete  FROM  test where id NOT in(  select id FROM test GROUP BY `name`  )

[Err] 1093 - You can't specify target table 'test' for update in FROM clause

要先執行create table tmp (select id FROM test GROUP BY `name` ) 

先建立臨時表,才能查詢