从mysql数据库删除重复记录只保留其中一条(保留id最小的一条)

摘要:
准备:创建新表tb_coupon/*NavicatPremiumDataTransferSourceServer:root@localhostSourceServerType:MySQLSourceServer版本:50527源主机:localhost:3306源架构:leyouTargetServerType:MySQLTargetServerVers
准备工作:新建表tb_coupon
/*
 Navicat Premium Data Transfer

 Source Server         : root@localhost
 Source Server Type    : MySQL
 Source Server Version : 50527
 Source Host           : localhost:3306
 Source Schema         : leyou

 Target Server Type    : MySQL
 Target Server Version : 50527
 File Encoding         : 65001

 Date: 22/05/2019 18:03:38
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for tb_coupon
-- ----------------------------
DROP TABLE IF EXISTS `tb_coupon`;
CREATE TABLE `tb_coupon`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '优惠卷id',
  `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '优惠卷名称',
  `type` enum('1','2','3') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '优惠卷类型,1、抵扣  2、折扣(打折)',
  `condition` bigint(20) NULL DEFAULT 0 COMMENT '抵扣或折扣条件,如果没有限制,则设置为0',
  `reduction` bigint(20) NULL DEFAULT 0 COMMENT '优惠金额',
  `discount` int(3) NULL DEFAULT 100 COMMENT '如果没有折扣,为100。如果是八五折,折扣为85',
  `targets` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '优惠券可以生效的sku的id拼接,以,分割',
  `stock` int(6) NOT NULL COMMENT '剩余优惠券数量',
  `start_time` datetime NOT NULL COMMENT '优惠券生效时间',
  `end_time` datetime NOT NULL COMMENT '优惠券失效时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '优惠卷表' ROW_FORMAT = Compact;

-- ----------------------------
-- Records of tb_coupon
-- ----------------------------
INSERT INTO `tb_coupon` VALUES (1, 'uuu', '1', 0, 0, 100, '', 2, '1000-01-01 00:00:00', '1000-01-01 00:00:00');
INSERT INTO `tb_coupon` VALUES (2, 'uuu', '1', 0, 0, 100, '', 2, '1000-01-01 00:00:00', '1000-01-01 00:00:00');
INSERT INTO `tb_coupon` VALUES (3, 'ddd', '2', 0, 0, 100, '', 2, '1000-01-01 00:00:00', '1000-01-01 00:00:00');
INSERT INTO `tb_coupon` VALUES (4, 'ddd', '2', 0, 0, 100, '', 2, '1000-01-01 00:00:00', '1000-01-01 00:00:00');
INSERT INTO `tb_coupon` VALUES (5, 'eee', '2', 0, 0, 100, '', 2, '1000-01-01 00:00:00', '1000-01-01 00:00:00');
INSERT INTO `tb_coupon` VALUES (6, 'eee', '3', 0, 0, 100, '', 2, '1000-01-01 00:00:00', '1000-01-01 00:00:00');

SET FOREIGN_KEY_CHECKS = 1;
1.查出重复的type
SELECT type FROM tb_coupon GROUP BY type HAVING count(type) > 1;
2.查出重复的type数据中最小的id
SELECT min(id) FROM tb_coupon GROUP BY type HAVING count(type) > 1;
3.查出重复的type数据中非最小的id(需要删除的)
SELECT id FROM tb_coupon WHERE type in(
    SELECT type FROM tb_coupon GROUP BY type HAVING count(type) > 1)
    AND id  not IN(SELECT min(id) FROM tb_coupon GROUP BY type HAVING count(type) > 1);
4.在Mysql中是不能删除查询出来的记录,而是要通过一张临时表来解决
SELECT id from (
    SELECT id FROM tb_coupon WHERE type in(
        SELECT type FROM tb_coupon GROUP BY type HAVING count(type) > 1)
        AND id  not IN(SELECT min(id) FROM tb_coupon GROUP BY type HAVING count(type) > 1)
) as t;
5.删除type重复的数据(只保留一条,保留最小id的)
DELETE FROM tb_coupon WHERE id IN (
    SELECT id from (
        SELECT id FROM tb_coupon WHERE type in(
            SELECT type FROM tb_coupon GROUP BY type HAVING count(type) > 1)
            AND id  not IN(SELECT min(id) FROM tb_coupon GROUP BY type HAVING count(type) > 1)
    ) as t
);

免责声明:文章转载自《从mysql数据库删除重复记录只保留其中一条(保留id最小的一条)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Clover 引导简明教程基于C++代码的UE4学习(四)—— 定时器下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

oracle pl/sql 变量

一、变量介绍在编写pl/sql程序时,可以定义变量和常量;在pl/sql程序中包括有:1)、标量类型(scalar)2)、复合类型(composite) --用于操作单条记录3)、参照类型(reference) --用于操作多条记录4)、lob(large object)    二、标量(scalar)——常用类型1)、在编写pl/sql块时,如果要使用...

navicat for mysql 进行数据传输

完成不同数据库之间的数据库表传输 把一个数据库表由一个数据库中复制到另一个数据库表中,可以使用heidisql 或 navicat for mysql,先将数据库表从一个数据库会话导出,然后会话连接相应库进行导入,类似一个备份的操作,但这样操作繁琐。navicat for mysql具备数据传输功能,直接将一个数据库的数据库表,复制到另一个数据库中。 1....

小白也能看懂的mySQL进阶【单表查询】

目录   1.查询基础 SELECT语句基础 列的查询  为列设定别名  常数的查询 过滤表中重复数据 根据WHERE语句来选择记录  注释的书写方法  算术运算符和比较运算符 算术运算符 需要注意NULL  比较运算符 对字符串使用不等号时的注意事项 不能对NULL使用比较运算符 逻辑运算符 NOT运算符  AND运算符和OR运算符 通过括号强化处理 逻...

阿里云Centos7安装mysql教程

1 基本安装过程 1.查看系统是否安装了mysql软件 # rpm -qa|grep -i mysql 2.将已经安装过的软件卸载掉。注意:这样的卸载是不彻底,不过这里够用了 # yum remove '软件名' 3.CentOS 7的yum源中默认是没有mysql的。所以,为了解决这个问题我们首先下载安装mysql的repo源。 # wget http:...

Mysql储存过程5: while

循环结构 while create procedure name() begin while 条件 do SQL语句 end while; end$ create procedure aa6() begin declare number int default 0; while num...

MySQL-快速入门(14)MySQL性能优化

1、MySQL性能优化包括查询速度优化、数据库结构优化、数据库服务器优化等。 优化的切入点:合理的结构设计、表结构、索引、查询语句。 2、show status查询数据库的性能参数 show status like 'value';value:Connections=>连接MySQL服务器的次数Uptime=>MySQL服务器的上线时间Slow...