Git Product home page Git Product logo

Comments (2)

abel533 avatar abel533 commented on September 26, 2024

Service层封装即可。

from mybatis-spring.

shenzhuan avatar shenzhuan commented on September 26, 2024

mysql 存在更新 不存在插入 2010-06-12 18:17:02
分类: Mysql/postgreSQL
看程序竟然发现Mysql有这个功能!
今天写程序,新发现……………………,相当不错^_^,省略了很多功夫,每天1G多的日志!!
MySQL 自4.1版以后开始支持INSERT … ON DUPLICATE KEY UPDATE语法,使得原本需要执行3条SQL语句(SELECT,INSERT,UPDATE),缩减为1条语句即可完成。
INSERT ... ON DUPLICATE KEY UPDATE,当插入的记录会引发主键冲突或者违反唯一约束时,则使用UPDATE更新旧的记录,否则插入新记录。
例如ipstats表结构如下:
CREATE TABLE ipstats (
ip VARCHAR(15) NOT NULL UNIQUE,
clicks SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0'
);
原本需要执行3条SQL语句,如下:
IF (SELECT * FROM ipstats WHERE ip='192.168.0.1') {
UPDATE ipstats SET clicks=clicks+1 WHERE ip='192.168.0.1';
} else {
INSERT INTO ipstats (ip, clicks) VALUES ('192.168.0.1', 1);
}
而现在只需下面1条SQL语句即可完成:
INSERT INTO ipstats VALUES('192.168.0.1', 1) ON DUPLICATE KEY UPDATE clicks=clicks+1;
注意,要使用这条语句,前提条件是这个表必须有一个唯一索引或主键。

from mybatis-spring.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.