查询以删除字符串中最后一个逗号后的所有字符

问题描述:

我有一个带有此类数据的mysql表

i have a mysql table with this sort of data

TACOMA, Washington, 98477

现在我有成千上万个这样的行.我希望以这样的方式处理数据:

Now i have thousands of such rows. I want the data to be manipulated in such a manner that it appears like:

TACOMA, Washington

是否可以通过mysql进行操作,还是必须手动进行操作?

Is it possible though mysql or do i have to manually do it.

您可以使用:

SELECT SUBSTRING_INDEX('TACOMA, Washington, 98477', ',', 2)

您可以在此处了解更多信息.

You can read more here.

和更新语句:

UPDATE my_table
    SET my_col = SUBSTRING_INDEX(my_col, ',', 2)

需要用表名替换my_table并用要更新的列替换my_col的地方.

Where you need to replace my_table with your table name and my_col with the column you need to be updated.