mysql-常用特殊sql的使用总结

一 重复数据的处理

1.1 使用表和数据的介绍

1.使用的是product_comment,商品评论表,表结构,以及语料,见下图

CREATE TABLE `product_comment` (
  `comment_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '评论ID',
  `product_id` int(10) unsigned NOT NULL COMMENT '商品ID',
  `order_id` bigint(20) unsigned NOT NULL COMMENT '订单ID',
  `customer_id` int(10) unsigned NOT NULL COMMENT '用户ID',
  `title` varchar(50) NOT NULL COMMENT '评论标题',
  `content` varchar(300) NOT NULL COMMENT '评论内容',
  `audit_status` tinyint(4) NOT NULL COMMENT '审核状态:0未审核1已审核',
  `audit_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '评论时间',
  `modified_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修改时间',
  PRIMARY KEY (`comment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品评论表';

入库脚本

insert into product_comment VALUES(44,131071,1,34,'商品不好','商品tmd',1,now(),now())

insert into product_comment VALUES(45,131072,2,56,'商品好','商品tmd',1,now(),now())

insert into product_comment VALUES(46,131072,2,56,'商品好','商品tmd2',1,now(),now())


insert into product_comment VALUES(47,131073,3,56,'商品omg','商品omg2',1,now(),now())

insert into product_comment VALUES(48,131073,3,56,'商品omg','商品omg2',1,now(),now())

insert into product_comment VALUES(49,131073,3,66,'商品omg','商品omg2',1,now(),now())
insert into product_comment VALUES(51,131073,3,76,'商品omg','商品omg2',1,now(),now())

insert into product_comment VALUES(50,131074,2,66,'商品omg','商品omg2',1,now(),now())

3.样本语料

 

 1.2 查询出重复数据

要求:查询出同一产品的同一商品的评论数据大于1条的数据

select product_id,order_id ,count(1) he from product_comment group by product_id,order_id having he>1

 1.2 删除重复数据

要求:删除同一产品的同一商品的评论数据大于1的数据,且保留评论id最小的那条数据(即保留重复数据的第一条)

1.2.1 方式一

1.先查询出:同一产品的同一商品的评论数据大于1的重复数据,id不是最新的那些数据的comment_id

select a.*,b.* from product_comment as a join   (
select product_id pid,order_id  oid ,count(1) he,min(comment_id) comment_id from product_comment group by product_id,order_id having he>1
) as b on a.product_id=b.pid and a.order_id=b.oid and  a.comment_id!=b.comment_id

2.根据comment_id进行删除

delete from product_comment where comment_id in (
select a.comment_id  from product_comment as a,
(
select product_id pid,order_id  oid ,count(1) he,min(comment_id) comment_id from product_comment group by product_id,order_id having he>1
) as b where a.product_id=b.pid and a.order_id=b.oid and a.comment_id!=b.comment_id

)

 可以看到报错:不能从同一张表查询出数据,又同时删除,需要在做一下修改:

delete from product_comment where comment_id in (
select c.comment_id from
(

select a.comment_id  from product_comment as a,
(
select product_id pid,order_id  oid ,count(1) he,min(comment_id) comment_id from product_comment group by product_id,order_id having he>1
) as b where a.product_id=b.pid and a.order_id=b.oid and a.comment_id!=b.comment_id
) as c
)

需要查询的数据再裹一层,成为临时表。

 1.2.2 方式二

1.先查询出:同一产品的同一商品的评论数据大于1的重复数据,id不是最新的那些数据的comment_id

select a.*,b.* from product_comment as a join   (
select product_id pid,order_id  oid ,count(1) he,min(comment_id) comment_id from product_comment group by product_id,order_id having he>1
) as b on a.product_id=b.pid and a.order_id=b.oid and  a.comment_id!=b.comment_id

 2.直接删除

delete a  ...的语法;delete 后面直接跟表名

delete a from product_comment as a join   (
select product_id pid,order_id  oid ,count(1) he,min(comment_id) comment_id from product_comment group by product_id,order_id having he>1
) as b on a.product_id=b.pid and a.order_id=b.oid
and a.comment_id!=b.comment_id

 查看表中的数据;

 可以看到实现了去除重复数据的删除。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值