删除双倍用户(MySQL)
我有两张桌子。有来自两个站点的用户信息: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建一个用户名唯一的新表,然后执行“插入忽略”...请参阅:
如何合并两个 MySQL 表?
Create a new table where the username is unique, then do an Insert Ignore... see:
How can I merge two MySQL tables?
将它们作为两个单独的语句进行。首先删除重复项:
然后使用 INSERT with SELECT声明:
Do them as two separate statements. First delete the duplicates with:
Then use the INSERT with SELECT statement:
试试这个
Try this
仅插入第二个表中的用户,其中第一个表中没有匹配的学生
only insert users from the second table where there is no matching student in the first table