mysql舊錶去重資料匯入新表且賦予主鍵id新值,mysql資料匯入

來源:互聯網
上載者:User

mysql舊錶去重資料匯入新表且賦予主鍵id新值,mysql資料匯入

業務需求:
A表有id,n1,n2,n3欄位,為建立空表,

B表有id,n1,n2,n3,n4,n5等欄位,為含有資料的舊錶,

現將B表中的n1,n2,n3組合的記錄去重後匯入A表中,並且A表主鍵也要錄入值

方案:將B表的記錄查詢後去重,同時對查記錄結果編排序號,將序號作為待插入A表的id值,sql語句如下:

INSERT INTO A表(id,n1,n2,n3) select @row :=@row+1 AS id,t1.* from (SELECT DISTINCT n1,n2,n3 FROM B表) t1,(select @row :=0) as t2

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.