有時候需要在表中更換一些字符串為NULL,可以使用下面的句子:
update tablename set fieldname = "textstring" where fieldname is null;
有時候需要進行替換操作
UPDATE `tablename`
SET `fieldname` = REPLACE(`fieldname`, '需要被替換的字符串', '需要替換的內容')
有時候在更新數據庫,只更新一些字段如timezone,zipcode字段,使用下面的句子:
這是python進行操作的例子
db = pymysql.connect(host='127.0.0.1',
port=3306,
user='root',
passwd='root',
db='datadata')
cursor = db.cursor()
insert_sql = """
INSERT INTO us_areacodeprefix(`id`, `areacodeprefix_id`, `linkname`, `timezone`, `latitude`, `longitude`, `zipcode`) VALUES (%s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE timezone=VALUES(timezone),zipcode=VALUES(zipcode)
"""
# 執行SQL語句
cursor.execute(insert_sql, (id, areacodeprefix_id,'',timezone,'','',zipcode))
# 提交到數據庫執行
db.commit()
其中id為主鍵,先讀取主鍵的id,然后再更新。
更新完畢后,改回原來的狀態即可。