準(zhǔn)備工作:新建表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 '優(yōu)惠卷id',
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '優(yōu)惠卷名稱',
`type` enum('1','2','3') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '優(yōu)惠卷類型,1、抵扣 2、折扣(打折)',
`condition` bigint(20) NULL DEFAULT 0 COMMENT '抵扣或折扣條件,如果沒有限制,則設(shè)置為0',
`reduction` bigint(20) NULL DEFAULT 0 COMMENT '優(yōu)惠金額',
`discount` int(3) NULL DEFAULT 100 COMMENT '如果沒有折扣,為100。如果是八五折,折扣為85',
`targets` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '優(yōu)惠券可以生效的sku的id拼接,以,分割',
`stock` int(6) NOT NULL COMMENT '剩余優(yōu)惠券數(shù)量',
`start_time` datetime NOT NULL COMMENT '優(yōu)惠券生效時(shí)間',
`end_time` datetime NOT NULL COMMENT '優(yōu)惠券失效時(shí)間',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '優(yōu)惠卷表' 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.查出重復(fù)的type
SELECT type FROM tb_coupon GROUP BY type HAVING count(type) > 1;
2.查出重復(fù)的type數(shù)據(jù)中最小的id
SELECT min(id) FROM tb_coupon GROUP BY type HAVING count(type) > 1;
3.查出重復(fù)的type數(shù)據(jù)中非最小的id(需要?jiǎng)h除的)
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中是不能刪除查詢出來的記錄,而是要通過一張臨時(shí)表來解決
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重復(fù)的數(shù)據(jù)(只保留一條,保留最小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
);