V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
cs5117155
V2EX  ›  MySQL

求一个数据不存在,则根据唯一索引插入对应的数据方法

  •  
  •   cs5117155 · 2021-07-08 10:54:39 +08:00 · 1282 次点击
    这是一个创建于 1016 天前的主题,其中的信息可能已经有所发展或是发生改变。
    CREATE TABLE `test` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `date` date DEFAULT NULL COMMENT '日期',
      `uid` int(11) unsigned DEFAULT NULL COMMENT '用户 id',
      `type` tinyint(1) DEFAULT '-1' COMMENT '类型: 1.代理 2.业务员',
      `order_type` tinyint(1) DEFAULT '1' COMMENT '1 订单 2.快递 3.商城',
      `new_num` int(11) DEFAULT '0' COMMENT '订单数量',
      `express_num` int(11) DEFAULT '0' COMMENT '快递数量',
      `amount` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '订单金额',
      `deposit` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '快递押金金额',
      `pay_amount` decimal(10,2) unsigned NOT NULL DEFAULT '0.00' COMMENT '付款金额',
      PRIMARY KEY (`id`),
    ) ENGINE=InnoDB COMMENT='统计表';
    

    date,uid,type,order_type,组合唯一索引,当数据库不存在这条唯一索引则插入,有则更新 new_num,deposit,pay_amount 相应字段。如何封装这个 sql 函数的方法,语言不限,最好提供 php,或者 js 的语言的版本

    2 条回复    2021-07-08 16:07:38 +08:00
    lerry
        1
    lerry  
       2021-07-08 10:56:40 +08:00
    lwep
        2
    lwep  
       2021-07-08 16:07:38 +08:00
    replace into 。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2781 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 13:24 · PVG 21:24 · LAX 06:24 · JFK 09:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.