mysql查询帮助(合并行)

Syl*_*rXS 0 mysql

由于程序中的错误,我的数据库中有一些半重复的数据.我想合并这些记录(或删除重复项).

我的数据如下:

usertable:
(userid, username, useremail)
101, joeuser, joeuser@mycompany
102, joeuser, joeuser@mycompany

datatable: 
(userid, datasubmitted)
101, mysubmittedata
102, othersubmitteddata
Run Code Online (Sandbox Code Playgroud)

我想摆脱任何重复的id并将任何id的任何记录合并到一个userid中.

完成后,我希望数据看起来像这样:

usertable:
(userid, username, useremail)
101, joeuser, joeuser@mycompany

datatable: 
(userid, datasubmitted)
101, mysubmittedata
101, othersubmitteddata
Run Code Online (Sandbox Code Playgroud)

d-l*_*ive 5

Its a two step process

1. fix your datatable first

Update datatable set userid = (select min(userid) from usertable group by username, useremail
    where username=datatable.username and useremail=datatable.useremail)



2. then remove duplicates from user table

delete from usertable u1 where userid > (select min(userid) from usertable u2 group by username, useremail
    where u1.username=u2.username and u1.useremail=u2.useremail)
Run Code Online (Sandbox Code Playgroud)