删除双倍用户(MySQL)

发布于 2024-08-29 18:55:35 字数 541 浏览 5 评论 0原文

我有两张桌子。有来自两个站点的用户信息:
p_users
p_users2
第一组有 3726 个用户,第二组有 13717 个用户。

p_users2 中的一些用户位于 p_users 中。我想将这两个表合并到一个大表中 - 但具有相同用户名的行不能加倍。

我该如何执行此操作?我尝试了这样的操作:

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
)

之后我应该收到一个具有唯一用户名的表,我想将其导出并导入到第一个表中。但是当我执行查询时出现错误:

SQL 错误 (1093):您无法在 FROM 子句中指定用于更新的目标表“p_users2”。 (MYSQL)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幽梦紫曦~ 2024-09-05 18:55:35

创建一个用户名唯一的新表,然后执行“插入忽略”...请参阅:

如何合并两个 MySQL 表?

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

How can I merge two MySQL tables?

孤者何惧 2024-09-05 18:55:35

将它们作为两个单独的语句进行。首先删除重复项:

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

然后使用 INSERT with SELECT声明:

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

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
蘑菇王子 2024-09-05 18:55:35

试试这个

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

Try this

DELETE p2 FROM p_pusers2 AS P2
INNER JOIN p_users p1
ON p1.username=p2.username
筑梦 2024-09-05 18:55:35

仅插入第二个表中的用户,其中第一个表中没有匹配的学生

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

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
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文