我有一张包含很多行的表(大约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)%" )
答案 0 :(得分:1)
您可以使用REPLACE()
功能
UPDATE city SET city_name = REPLACE(city_name, '(CD)', '')
WHERE city_name LIKE( "%(CD)%" )
答案 1 :(得分:1)
如果它总是以“(CD)”结尾,你想要将它们砍掉,你可以这样做:
UPDATE city
SET city_name = LEFT(city_name, CHAR_LENGTH(city_name) - 5)
WHERE city_name LIKE '% (CD)';