更新行在列中重复的行?

时间:2019-04-15 23:16:32

标签: mysql sql duplicates

我正在使用一个相当大的城市数据库。 我有几排重复的城市,因为它们来自不同的国家/州。

我需要做的是: 重复的城市以这种方式连接 列中的(ascii_name + "_"+ country_code)name_city

仅重复的城市。

使用此过滤器代码重复的代码:

SELECT ascii_name FROM cities GROUP BY ascii_name HAVING count (ascii_name)> 1

例如,带有“罗马”的(img),我得到3行,我想在带有“国家/地区代码”的那一列“ name_city”中加以区分

(是mysql)

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:0)

不确定这就是您要寻找的东西。

串联语法因数据库而异。 ||在ANSI串联运算符中运行,在Oracle中运行,+在SQL Server中运行,CONCAT函数在MySQL中运行。

SELECT CONCAT(ascii_name,'_',country_code) as name_city from cities 
where ascii_name in (select ascii_name FROM cities GROUP BY ascii_name HAVING count (ascii_name)> 1)

答案 1 :(得分:0)

使用UPDATE查询,并将其与子查询结合在一起,以查找所有重复的名称。

UPDATE cities AS c1
JOIN (SELECT ascii_name
      FROM cities
      GROUP BY ascii_name
      HAVING COUNT(*) > 1) AS c2
    ON c1.ascii_name = c2.ascii_name
SET c1.name_city = CONCAT(c1.ascii_name,'_',c1.country_code)

答案 2 :(得分:-1)

我认为您的子查询应该运行正常

select ascii_name+"_"+country_code as name_city
from cities
where ascii_name in
(SELECT ascii_name FROM cities GROUP BY ascii_name HAVING count (ascii_name)> 1);