我有一个表格,它有很多行,看起来像下面的东西。
id Name
1481 Carrier_186_CLI
1469 Carrier_186_OUT
1480 Carrier_107_CLI
1483 Carrier_107_OUT
我想要的只是将行复制到另一个具有常用短语的表中,例如
id Name
1 Carrier_186
2 Carrier_107
我试过这个
Insert into newtable(Name)select Name from oldtable group by Name
我知道它行不通,但我无法为此找到解决方案。我是 mysql 的新手。
您可以使用字符串函数substring_index
在第二个下划线之前选取值
Insert into newtable(Name)
select substring_index(Name,'_',2)
from oldtable
group by substring_index(Name,'_',2)
演示