如果它不存在,则添加到 MySQL 中的行 [英] Adding to row in MySQL if it doesn't exist

查看:44
本文介绍了如果它不存在,则添加到 MySQL 中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向表中添加一行,但首先检查它是否存在时,哪种处理方法最有效?

When adding a row to a table, but first checking to see if it exists first Which would be the most efficient way of handling this?

会不会是查询的情况,看是否存在,如果不存在则插入.

Would it be a case of query to see if it exist, if not then insert.

还是重复使用?

或者简单地替换(如果该行不存在,这行得通吗)?

Or simply replace (Would this work, if the row did not exist)?

谢谢

推荐答案

我认为这是 MySQL 中最快的方法:

I think this is the fastest way in MySQL:

REPLACE into table (col1, col2) values(1, 'ABC')

如果该行确实存在,MySQL 将删除该行并插入一个新行.

MySQL will delete the row if it does exist and insert a new one.

这篇关于如果它不存在,则添加到 MySQL 中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆