Home > Article > Backend Development > Several methods to find and replace fixed strings in mysql fields_PHP tutorial
Our requirement is to remove China from the address field and set the province (province_id) field according to the address field.
First, to find a string, we can use replace in mysql, which is also mentioned in this blog. For details, see: http://www.jb51.net/article/31374.htm
Okay, let’s remove the character "China".
update table set address = replace(address,'China','')
Second, update the province_id field based on the first characters of the address field. The SQL statement is as follows
UPDATE table SET province_id=11 where LEFT(address,2)='Fujian'
The LEFT function of mysql is used here to find the first two characters of the address character and determine whether it is 'Fujian' If so, SQL will update the province_id field to 11. The 11 here is the ID corresponding to the province table Fujian.