0

我有一张有很多行的表(大约 1,500 行)。在 city_name 列中,一些单元格具有城市名称并以 (CD) 结尾。我希望那些带有 (CD) 的单元格在没有 (CD) 的情况下更新。这是我的表的预览:

+---------+-----------------------+---------+
| city_id | city_name             | id_dept |
+---------+-----------------------+---------+
|       1 | El Encanto (CD)       |       1 |
|       2 | La Chorrera (CD)      |       1 |
|       3 | La Pedrera (CD)       |       1 |
|       4 | La Victoria (CD)      |       1 |
|       5 | Leticia               |       1 |
|       6 | Miriti Parana (CD)    |       1 |
|       7 | Puerto Alegria (CD)   |       1 |
|       8 | Puerto Arica (CD)     |       1 |
|       9 | Puerto Nariño         |       1 |
|      10 | Puerto Santander (CD) |       1 |
+---------+-----------------------+---------+
10 rows in set (0.00 sec)

这就是我想要的:

+---------+-----------------------+---------+
| city_id | city_name             | id_dept |
+---------+-----------------------+---------+
|       1 | El Encanto            |       1 |
|       2 | La Chorrera           |       1 |
|       3 | La Pedrera            |       1 |
|       4 | La Victoria           |       1 |
|       5 | Leticia               |       1 |
|       6 | Miriti Parana         |       1 |
|       7 | Puerto Alegria        |       1 |
|       8 | Puerto Arica          |       1 |
|       9 | Puerto Nariño         |       1 |
|      10 | Puerto Santander      |       1 |
+---------+-----------------------+---------+
10 rows in set (0.00 sec)

好吧,我知道我可以使用 (CD) 选择所有行:

SELECT * FROM city WHERE city_name LIKE ( "%(CD)%" )

但是如何删除(CD):

UPDATE city SET city_name = "" WHERE city_name LIKE( "%(CD)%" )
4

2 回答 2

1

您可以使用该REPLACE()功能

UPDATE city SET city_name = REPLACE(city_name, '(CD)', '') 
WHERE city_name LIKE( "%(CD)%" )
于 2012-09-03T17:50:53.300 回答
1

如果它总是以 " (CD)" 结尾并且你想把它们从末尾砍掉,你可以这样做:

UPDATE city
SET city_name = LEFT(city_name, CHAR_LENGTH(city_name) - 5)
WHERE city_name LIKE '% (CD)';
于 2012-09-03T17:52:03.480 回答