replace into 跟 insert 功能類似,不同點(diǎn)在于:replace into 首先嘗試插入數(shù)據(jù)到表中。本文主要介紹了mysql 的replace into實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下,希望能幫助到大家。
mysql 的replace into實(shí)例詳解
1、如果發(fā)現(xiàn)表中已經(jīng)有此行數(shù)據(jù)(根據(jù)主鍵或者唯一索引判斷)則先刪除此行數(shù)據(jù),然后插入新的數(shù)據(jù)。
2、 否則,直接插入新數(shù)據(jù)。
要注意的是:插入數(shù)據(jù)的表必須有主鍵或者是唯一索引!否則的話,replace into 會(huì)直接插入數(shù)據(jù),這將導(dǎo)致表中出現(xiàn)重復(fù)的數(shù)據(jù)。?
MySQL中replace into有三種寫法:
?代碼如下:
?replace?into?table(col,?...)?values(...) ?replace?into?table(col,?...)?select?... ?replace?into?table?set?col=value,?...
擴(kuò)展: mysql得到對(duì)應(yīng)插入的最后主鍵(一般用戶獲取訂單表的主鍵訂單號(hào))
SELECT?LAST_INSERT_ID()?from?dual
相關(guān)推薦:
MySQL中REPLACE INTO和INSERT INTO的區(qū)別分析
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載。
THE END