常用批量增删改查sql处理案例

批量新增sql案例:
insert into 表名(字段名1,2,3...(select 字段1,2,3... from xxx where xxx)

批量修改sql案例:
UPDATE t_hospital_depart_brand_history t2 ,t_hospital_depart_brand t1 SET t2.histroy_flag = t1.histroy_flag
where (t1.institutionno=t2.institutionno and t1.depno=t2.depno and t1.medicineno=t2.medicineno) and t2.histroy_flag != t1.histroy_flag

批量修改sql案例:
UPDATE t_hospital_depart_brand SET month =
		CASE
        month 
        WHEN '2023年3月' THEN
        '2023年03月' 
        WHEN '2023年4月' THEN
        '2023年04月' 
    END 
		where (month = '2023年3月' or month = '2023年4月')


批量删除sqldelete from `t_hospital_depart_brand` where id in(
SELECT a.id FROM (SELECT id FROM (
SELECT * FROM t_hospital_depart_brand t1 WHERE ((SELECT COUNT(*) FROM t_hospital_depart_brand t2 WHERE t1.institutionno=t2.institutionno and t1.depno=t2.depno and t1.medicineno=t2.medicineno and t1.month=t2.month) > 1)
) t3 where updtimestamp=(SELECT min(updtimestamp) from t_hospital_depart_brand as t4 WHERE t3.institutionno=t4.institutionno and t3.depno=t4.depno and t3.medicineno=t4.medicineno GROUP BY  institutionno,depno,medicineno)
)a
);

只查重复数据sqlSELECT * FROM t_hospital_depart_brand_history t1 WHERE ((SELECT COUNT(*) FROM t_hospital_depart_brand_history t2 WHERE t1.institutionno=t2.institutionno and t1.depno=t2.depno and t1.medicineno=t2.medicineno and t1.month=t2.month) > 1)

查询重复数据中修改时间小的那个
SELECT * FROM (
SELECT * FROM t_hospital_depart_brand_history t1 WHERE ((SELECT COUNT(*) FROM t_hospital_depart_brand_history t2 WHERE t1.institutionno=t2.institutionno and t1.depno=t2.depno and t1.medicineno=t2.medicineno and t1.month=t2.month) > 1)
) t3 where updtimestamp=(SELECT min(updtimestamp) from t_hospital_depart_brand_history as t4 WHERE t3.institutionno=t4.institutionno and t3.depno=t4.depno and t3.medicineno=t4.medicineno GROUP BY  institutionno,depno,medicineno)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值