mysql保存数据之存在就更,否则就保存数据
分享一篇关于在mysql中对数据保存时如果存在就更新数据,否则就保存数据哦,很实用的,有需要的朋友可以参考一下。
//如果不存在,则插入新数据
代码如下 | 复制代码 |
$sql = "INSERT INTO {$ecs->table(‘cat_lang')} (cat_id,lang_id,cat_name,keywords,cat_desc) VALUES({$cat_id},{$k},'{$val['cat_name']}','{$val['keywords']}','{$val['cat_desc']}') ON DUPLICATE KEY UPDATE cat_name='{$val['cat_name']}',cat_desc='{$val['cat_desc']}',keywords='{$val['cat_desc']}'"; |
这里要注意的是:必须表主键唯一 。这里,这里我采用的是 分类id和语言id双主键机制。
key为唯一键或者主键
(1) key存在则替换,不存在则插入
代码如下 | 复制代码 |
replace into t_test | CREATE TABLE `t_test` ( `ikey` int(11) NOT NULL default '0', `value` varchar(21) NOT NULL default '', `icount` int(11) NOT NULL default '0', PRIMARY KEY (`ikey`) ) ENGINE=MyISAM DEFAULT CHARSET=gbk | |
对于这样一个表.当要记录ikey=1,value='a',icount=0时,由于不知道表里有没有ikey=1的项.我们可能会先,然后看有没有结果,如果有,则使用update进行更新.如果没有,则使用insert进行插入.
不过在大并发量的数据操作时,可能有时一个有主键的select查询都要用上2s,如果对旧数据不关心,减少不必要的操作显得犹为重要.
使用replace一个语句可以完成上面两个语句的功能,其语法与insert差不多.如上面的操作可以写为replace into t_test set ikey=1,value='a',icount=0;则表中有ikey为1时,先删除旧数据.然后插入新数据.否则直接插入数据.
(2) key存在则更新,不存在则插入
代码如下 | 复制代码 |
insert into .... on duplicate key update |
对于需要根据原记录进行操作的.如表中icount字段用于计数,当没有记录时,插入的value为0,当有记录时,value需要更新为value+1,这时replace就不能完成这个功能.使用insert则可以,其基本语法是insert into ... on duplicate key update...,如上述语句为
代码如下 | 复制代码 |
insert into t_test set ikey=1,value='a',value2=1 on duplicate key update value2=value2+1; |
如果表中有多个唯一索引,如对value字段加个unique key.这时表中有ikey和value两个唯一索引,replace会把所有与其唯一索引值相同的数据项删除,再插入新记录.如表中有两个记录
代码如下 | 复制代码 |
+------+-------+--------+ | ikey | value | icount | +------+-------+--------+ | 2 | a | 10 | | 1 | b | 40 | +------+-------+--------+ 则replace into t_test set ikey=1,value='a',icount=0;会把表中的两条记录都删除,然后插入新记录. 而insert into t_test set ikey=1,value='a',icount=0 on duplicate key update icount=icount+1则只更新一条记录.其效果相当于update t_test set icount=icount+1 where ikey=1 or value='a' limit 1; |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PrimeFactor−Innumbertheory,theprimefactorsofapositiveintegeraretheprimenumbersthatdividethatintegerexactly.Theprocessoffindingthesenumbersiscalledintegerfactorization,orprimefactorization.Example−Primefactorsof288are:288=2x2x2x2x2

In 2025, global digital virtual currency trading platforms are fiercely competitive. This article authoritatively releases the top ten digital virtual currency trading platforms in the world in 2025 based on indicators such as transaction volume, security, and user experience. OKX ranks first with its strong technical strength and global operation strategy, and Binance follows closely with high liquidity and low fees. Platforms such as Gate.io, Coinbase, and Kraken are at the forefront with their respective advantages. The list covers trading platforms such as Huobi, KuCoin, Bitfinex, Crypto.com and Gemini, each with its own characteristics, but investment should be cautious. To choose a platform, you need to consider factors such as security, liquidity, fees, user experience, currency selection and regulatory compliance, and invest rationally

Ranking of the top ten virtual currency trading platforms (latest in 2025): Binance: Global leader, high liquidity, and regulation has attracted attention. OKX: Large user base, supports multiple currencies, and provides leveraged trading. Gate.io: A senior exchange, with a variety of fiat currency payment methods, providing a variety of trading pairs and investment products. Bitget: Derivatives Exchange, high liquidity, low fees. Huobi: An old exchange that supports a variety of currencies and trading pairs. Coinbase: A well-known American exchange, strictly regulated. Phemex and so on.

Top 10 digital currency trading platforms: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini, these exchanges have their own characteristics, and users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

Reliable digital currency platforms include: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini. These exchanges have their own characteristics. Users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

This article recommends ten digital currency trading apps: 1. OKX; 2. Binance; 3. Gate.io; 4. Huobi Global; 5. Kraken; 6. Coinbase; 7. KuCoin; 8. Crypto.com; 9. Bitfinex; 10. Poloniex. When choosing a platform, you need to consider factors such as security, liquidity, transaction fees, currency selection, user interface, customer service support and regulatory compliance, and carefully evaluate risks and never blindly follow the trend.

This article provides Android and Apple mobile APP download methods for mainstream digital currency trading platforms such as Binance, OKX, Gate.io, Huobi Global, Coinbase, KuCoin, Kraken and Bitfinex. Whether it is an Android user or an Apple user, you can easily find the official APP download link for the corresponding platform and complete the installation according to the steps. The article provides detailed guidance on searching and downloading on their respective official websites or app stores, and provides instructions on the special steps for installing APK files on Android, so that users can download and use them quickly and easily.

The rankings of the top ten virtual currency trading platforms are: 1. OKX; 2. Binance; 3. Gate.io; 4. Huobi Global; 5. Kraken; 6. Coinbase; 7. KuCoin; 8. Crypto.com; 9. Bitfinex; 10. Gemini. The ranking is based on comprehensive considerations such as platform liquidity, currency selection, security, user experience, handling fees and compliance, but is for reference only. Investment should be cautious and at your own risk.
