测试用的mysql数据库: 新建测试表: CREATE TABLE `test` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `text` varchar(255) DEFAULT NULL, `uid` varchar(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uid_unique` (`uid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 以上建表三个字段,i…
MySQL 对 SQL 有很多扩展,有些用起来很方便,但有一些被误用之后会有性能问题,还会有一些意料之外的副作用,比如 REPLACE INTO. 比如有这样一张表: CREATE TABLE `auto` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `k` ) unsigned NOT NULL, `v` ) DEFAULT NULL, `extra` ) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uk…
MySQL replace into 说明(insert into 增强版) 在插入数据到一个表时,通常是这种情况:1. 先推断数据是否存在: 2. 假设不存在,则插入:3.假设存在,则更新. 在 SQL Server 中能够这样处理: if not exists (select 1 from t where id = 1) insert into t(id, update_time) values(1, getdate()) else update t set update_time = ge…
在使用SQL语句进行数据表插入insert操作时,如果表中定义了主键,插入具有相同主键的记录会报错:  Error Code: 1062. Duplicate entry 'XXXXX' for key 'PRIMARY'(主键冲突) 这样我们只好检查主键是不是存在,若存在则更新,若不存在则插入.或者写SQL流程控制语句(if...else...) Mysql提供了replace into跟insert into的功能一样.不同点在于: 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先…
replace into的存在的几种情况 当表存在主键并且存在唯一键的时候 如果只是主键冲突 mysql> select * from auto; +----+---+------+---------+ | id | k | v | extra | +----+---+------+---------+ | 2 | 2 | 2 | extra 2 | | 3 | 3 | 3 | extra 3 | | 4 | 1 | 1-1 | NULL | +----+---+------+---------…
mysqlsql serverinsert 在向表中插入数据的时候,经常遇到这样的情况:1. 首先判断数据是否存在: 2. 如果不存在,则插入:3.如果存在,则更新. 在 SQL Server 中可以这样处理: if not exists (select 1 from t where id = 1)       insert into t(id, update_time) values(1, getdate())    else       update t set update_time =…
下面我们主要说一下在插入时候的几种情况: 1:insert ignore 2:replace into 3:ON DUPLICATE KEY UPDATE 关于insert ignore: 关于replace into: 关于ON DUPLICATE KEY UPDATE : MySQL 对 SQL 有很多扩展,有些用起来很方便,但有一些被误用之后会有性能问题,还会有一些意料之外的副作用,比如 REPLACE INTO. 比如有这样一张表: CREATE TABLE `auto` ( `id`…
讨人喜欢的 MySQL replace into 用法(insert into 的增强版) 在向表中插入数据的时候,经常遇到这样的情况:1. 首先判断数据是否存在: 2. 如果不存在,则插入:3.如果存在,则更新. 在 SQL Server 中可以这样处理: ) , getdate()) else 那么 MySQL 中如何实现这样的逻辑呢?别着急!mysql 中有更简单的方法: replace into , now()); 或 , now(); replace into 跟 insert 功能类…
mysql replace实例说明: UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def'); 释:表tb1中字段f1中为abc的值更新为def.一般用于某字段中值存在不合适的字段,需要批量改变一下,可以用到update table set 字段=replace('字段','字段的某值','需要替换的值'); REPLACE(str,from_str,to_str) 在字符串 str 中所有出现的字符串 from_str 均被 to_str替换,然后返回这个字符串…
转 http://blog.csdn.net/risingsun001/article/details/38977797 MySQL replace into 用法(insert into 的增强版) 在向表中插入数据的时候,经常遇到这样的情况:1. 首先判断数据是否存在: 2. 如果不存在,则插入:3.如果存在,则更新. 在 SQL Server 中可以这样处理: if not exists (select 1 from t where id = 1) insert into t(id, up…