MySQL语句判断数据库数据重复情况,新增、删除、不变。

判断 7月8月两个月数据对比情况,新增、删除(离职)、重复。

 根据manager_name,gg_name,employer,department,historical_office判断出是否重复数据

-- ●- 新增或离职
-- ●- 创建临时表

CREATE TABLE temp_table  (
        SELECT id,manager_name,gg_name,employer,department,historical_office,create_time,update_time
        FROM `organization_executive_position`
        GROUP BY manager_name,gg_name,employer,department,historical_office
        HAVING COUNT(*) =1  
);
select * from temp_table;

-- ●- 离职人员
UPDATE `organization_executive_position` t1 right join temp_table t2 on t1.id=t2.id
SET t1.zhuangtai = '1' 
WHERE t1.create_time < '2023-08-01 00:00:00'
-- ●- 新增人员
UPDATE `organization_executive_position` t1 right join temp_table t2 on t1.id=t2.id
SET t1.zhuangtai = '2' 
WHERE t1.create_time > '2023-08-01 00:00:00'
-- 删除临时表
DROP TABLE temp_table;

-- ●- 删除重复数据
DELETE FROM `organization_executive_position` WHERE id NOT IN (
  SELECT t.max_id FROM (
        -- 查询出ID最大的保留下来
    SELECT MAX(id) AS max_id
    FROM `organization_executive_position` 
    GROUP BY manager_name,gg_name,employer,department,historical_office
  ) t
);    
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值