Mysql 如何将重复名称的单个匹配项插入到另一个表中?

Mysql 如何将重复名称的单个匹配项插入到另一个表中?,mysql,Mysql,我需要将每个重复名称中的一个从一个表插入到另一个表中。 只有副本 以下查询列出了所有重复的名称和计数: select name, count(name) as cnt from my_table group by name having cnt > 1 order by name 如何将每个事件中的一个插入到另一个表中 更新 我的桌子不一样。 我的新表只有以下行: id (auto increment) name (varchar) 首先创建表 然后将数据插入新表: inser

我需要将每个重复名称中的一个从一个表插入到另一个表中。
只有副本

以下查询列出了所有重复的名称和计数:

select name, count(name) as cnt
from my_table
group by name
having cnt > 1
order by name
如何将每个事件中的一个插入到另一个表中

更新

我的桌子不一样。 我的新表只有以下行:

id (auto increment)  
name (varchar)  
首先创建表

然后将数据插入新表:

insert into new_table 
select name, count(name) as cnt
from my_table
group by name
having count(name) > 1
order by name
有关详细信息,请咨询

已编辑

可以指定列名称或顺序,例如:

insert into new_table (column1, column2)
select name, count(name) as cnt
...
对于您的表,您需要创建另一个字段来存储
cnt

alter table `new_table` add column cnt int;

insert into new_table (name, cnt)
select name, count(name) as cnt
...
首先创建表

然后将数据插入新表:

insert into new_table 
select name, count(name) as cnt
from my_table
group by name
having count(name) > 1
order by name
有关详细信息,请咨询

已编辑

可以指定列名称或顺序,例如:

insert into new_table (column1, column2)
select name, count(name) as cnt
...
对于您的表,您需要创建另一个字段来存储
cnt

alter table `new_table` add column cnt int;

insert into new_table (name, cnt)
select name, count(name) as cnt
...

如果需要,您可以在表名之后指定列名,如
(name,name\u count)
。对不起,我忘了说,这些表不相同。我尝试指定列,但随后收到错误消息
列计数与第1行的值计数不匹配
您几乎得到了它。ID具有自动增量,因此无法在其中插入。如果您从select中删除
count(名称为cnt)
,它会起作用:)@danihp谢谢。我不存储计数,只存储名称。因此,如果删除select:)中的“count”部分,则第一个示例是正确的。如果需要,可以在表名后面指定列名,如
(name,name\u count)
。对不起,我忘了说,表不相同。我尝试指定列,但随后收到错误消息
列计数与第1行的值计数不匹配
您几乎得到了它。ID具有自动增量,因此无法在其中插入。如果您从select中删除
count(名称为cnt)
,它会起作用:)@danihp谢谢。我不存储计数,只存储名称。因此,如果删除select:)中的“计数”部分,则第一个示例是正确的