MySQL中替换、更新字段及其它
有时候需要在表中更换一些字符串为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,然后再更新。
更新完毕后,改回原来的状态即可。