Mysql删除重复数据、比较两个表数据

mysql 专栏收录该内容
3 篇文章 0 订阅

#1.删除t_discredit_company_info表重复数据(t_discredit_company_info表数据重复)

DELETE FROM t_discredit_company_info WHERE id IN (

SELECT * FROM (

SELECT id FROM t_discredit_company_info WHERE (company_id,attribute_year_month)

IN ( SELECT company_id,attribute_year_month FROM t_discredit_company_info GROUP BY company_id,attribute_year_month HAVING COUNT(1) > 1

) AND id NOT IN (

-- 查询最小id的记录

SELECT MIN(id) FROM t_discredit_company_info GROUP BY company_id,attribute_year_month HAVING COUNT(1) > 1

)) AS stu_repeat_copy );

#2.查询t_discredit_company表比t_discredit_company_info表多了哪些数据(t_discredit_company表数据多,t_discredit_company_info表数据少)

SELECT count(*) FROM t_discredit_company o LEFT JOIN t_discredit_company_info w ON o.company_id=w.company_id

WHERE w.company_id IS NULL;

#3.删除t_discredit_company表的多余数据(t_discredit_company表数据多,t_discredit_company_info表数据少)

delete from t_discredit_company where t_discredit_company.company_id not in

(select company_id from t_discredit_company_info);

  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值