2012-10-10 80 views
-1

我有MySQL基础表,并需要更改一列中的东西。MySQL:如何删除一列中的一部分条目?

项在该列有:

Cat - Grey - small 
Cat - Grey - middle 
Dog - Grey - big 
Elephant - Grey - big 

我需要删除中间字,这样我就可以得到猫 - 小,猫 - 中间..

所以这是一列。

+0

使用正则表达式来提取的第一个和最后一个字,设定/更新您的场结果 – Michael

回答

1

您可以使用REPLACE()

UPDATE table SET column = REPLACE('- Grey -', '-', column); 
+0

UPDATE'table' SET'column' = REPLACE(' - Gray -',''); 这工作!谢谢! –

1

这里有一个通用的方法来获取字符串没有中间字:

select concat(
    substring(oldcol,1,locate('-', oldcol)), 
    substring(oldcol,length(oldcol) - locate("-", reverse(oldcol))+2) 
) as newcol 
from mytable