11

My 'Location' table contains 6 columns. (ID, Name, Alias, Area, X, Y)

Example of some entries from the 'Name' column:

Blackwood Highschool, Paris, France

Hilltop Market, Barcelona, Spain

Roundwell Plaza, Melbourne, Australia

Rurk Mount, Moscow, Russia(mountain)

History Museum, Prague, Czech Republic

Narrow River (river), Bombay, India

Some entries include "(mountain)", "(river)" or "(...)" within the name (8 different ones). I don't know why the table was created this way. It should have had an extra column for this data, but well.

I want to remove just the "(...)" substrings from the Location Names. I don't know how to do it, it's something like this so you get an idea:

DELETE FROM 'Location' 
WHERE 'Name'
LIKE '%(%)%';

I know this would delete the whole row, but I just want to remove the (%) term from the 'Name' string.

4

4 回答 4

34

如果您只有 8 个变体,而且这是一次性的,您可以通过替换来完成。

update location
  set name = replace(name , '(river)','')
  where name like '%(river)%';
于 2013-06-21T20:37:52.830 回答
3

您可以通过蛮力字符串操作来做到这一点:

select concat(left(name, instr(name, '(') - 1),
              right(name, length(name) - instr(val, ')'))
             )

实际上,您希望在update声明中这样做:

update location
    set name = concat(left(name, instr(name, '(') - 1),
                      right(name, length(name) - instr(val, ')'))
                     )
    where name like '%(%)%';

想要delete,因为这会删除整行。

于 2013-06-21T20:31:08.987 回答
0

我为你做了这个例子:

set @name = "Rurk Mount, Moscow, Russia(mountain)";

set @fir = locate("(",@name);
set @las = locate(")",@name);

set @lef = left(@name,@fir-1);

set @word = substr(@name,@fir+1,@las-@fir-1);

select @fir,@las,@lef,@word,concat(@lef,' ',@word);

通过这种方式,您可以看到完成任务的方法。

于 2013-06-21T20:40:24.760 回答
0

代替 DELETE,执行 UPDATE,例如:

UPDATE Location
SET Name = LEFT(Name,LOCATE('(',Name)-1)
WHERE LOCATE('(',Name) > 0

我认为语法有点不对劲。

于 2013-06-21T20:28:05.453 回答