use jinan_babi SELECT * FROM TImpMember DROP TABLE TMember_Unique SELECT MIN(FCreateDate) AS FCreateDate, FName,FMobilePhone,SUM(1) AS FCount,CAST(0 AS BIGINT) AS FNewMemId INTO TMember_Unique FROM TMember WHERE FMobilePhone > '' AND FMemId NOT IN(SELECT FMemId FROM TConsumeBill) AND FMemId NOT IN(SELECT FMemId FROM TMemberSaleBill) GROUP BY FName,FMobilePhone HAVING(SUM(1) > 1) --更新memId UPDATE A SET A.FNewMemId = B.FMemId FROM TMember_Unique AS A INNER JOIN TMember AS B ON B.FMobilePhone = A.FMobilePhone AND A.FName = B.FName WHERE B.FCreateDate = A.FCreateDate AND B.FMemId NOT IN(SELECT FMemId FROM TConsumeBill) AND B.FMemId NOT IN(SELECT FMemId FROM TMemberSaleBill) --获取达到条件的会员 SELECT A.FMemId,A.FMobilePhone,A.FName,B.FNewMemId INTO TMember_Update FROM TMember AS A,TMember_Unique AS B WHERE B.FMobilePhone = A.FMobilePhone AND A.FName = B.FName ORDER BY B.FMobilePhone --开始更新 UPDATE A SET A.FMemId = B.FNewMemId FROM TMemberCard AS A,TMember_Update AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId UPDATE A SET A.FMemId = B.FNewMemId FROM TMemberCourse AS A,TMember_Update AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId UPDATE A SET A.FMemId = B.FNewMemId FROM TMemberCourseLog AS A,TMember_Update AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId SELECT * FROM TMember WHERE FName = '纪玲' SELECT * FROM TMemberCourse WHERE FMemId = 6376533 USE JINAN_BABI UPDATE A SET A.FMemId = B.FNewMemId FROM TMemberCardAccount AS A,TMember_Update AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId UPDATE A SET A.FMemId = B.FNewMemId FROM TMemberCardAcctHist AS A,TMember_Update AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId UPDATE A SET A.FMemId = B.FNewMemId FROM TMemberTransHist AS A,TMember_Update AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId UPDATE A SET A.FMemNO = FMemNo + '相同删除', A.FDeleted = 1 FROM TMember AS A,TMember_Update AS B WHERE A.FMemId = B.FMemId AND A.FMemId != B.FNewMemId SELECT * FROM TMember_Update SELECT * FROM TConsumeBill ORDER BY FBillTime DESC