mysql如果找不到列值则插入,找到值时忽略。

sql语句

1
INSERT into user_table(uid, name) select 'uid1', 'uname1' from dual WHERE NOT EXISTS (select name from user_table where name='uname1');

python 格式化代码

1
2
3
"INSERT into user_table(uid, name) select '{}', '{}' from dual WHERE NOT EXISTS (select name from user_table where name='{}')".format(
uid, name, name
)