DELETING doubled users (MySQL)

混江龙づ霸主 提交于 2019-12-25 02:46:35

问题


I have two tables. There are users informations from two sites:
p_users
p_users2
There are 3726 users in first and 13717 in second.

Some users in p_users2 are in p_users. I want merge this two tables to the one big table - but rows with same usernames can't be doubled.

How can I do this? I tried something like this:

DELETE FROM p_users2 WHERE user_id IN 
(
select p.user_id from p_users p
join p_users2 p2 on p.username=p2.username
)

After that I should receive a table with unique usernames, which I want to export and import to the first one. But when I execute my query I got error:

SQL Error (1093): You can't specify target table 'p_users2' for update in FROM clause. (MYSQL)


回答1:


Try this

DELETE p2 FROM p_pusers2 AS P2
INNER JOIN p_users p1
ON p1.username=p2.username



回答2:


Create a new table where the username is unique, then do an Insert Ignore... see:

How can I merge two MySQL tables?




回答3:


Do them as two separate statements. First delete the duplicates with:

DELETE FROM p_users2 WHERE user_id IN 
(select p.user_id from p_users p)

Then use the INSERT with SELECT statement:

INSERT INTO P_USERS (FIELD1, FIELD2, FIELD3) SELECT FIELD1, FIELD2, FIELD3 FROM P_USERS2



回答4:


only insert users from the second table where there is no matching student in the first table

INSERT INTO p_users
SELECT * FROM p_users2 p2
WHERE NOT EXISTS (
  SELECT * FROM p_users p1
  WHERE p1.id = p2.id
)


来源:https://stackoverflow.com/questions/2669165/deleting-doubled-users-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!