【无标题】

这篇博客记录了一种优化数据库设计的方法,将原本存储中文名称的字段facility改为存储facility表中id的拼接字符串。通过内联查询和GROUP_CONCAT函数,作者实现了对room_types表中facility字段的更新,确保数据平滑过渡。

用子查询的结果更新表字段

有的时候我们在做数据库设计的时候,早期设计的字段存储中文名称的拼接字符串。
后期,发现这样的设计很不方便,我们需要把这个字段改成存储id拼接的字符串。
嗯,这时候要处理数据了,需要把facility这个字段的数据更改成 facility表查询出来的id字段拼接字符串。如下是我实操的代码留个纪念。

查询优化版
select s.facility,c.facility_id ,s.id from room_type s
INNER JOIN
(
select a.id,a.facility,case when GROUP_CONCAT(b.id) is null then ‘’ else GROUP_CONCAT(b.id) end as facility_id ,b.id as b_id from room_type as a
left join facility b on a.store_id = b.store_id and find_in_set(b.name,a.facility)
group by a.id
) c
ON s.id = c.id

//成功版
update room_type s
INNER JOIN
(
select a.id,a.facility,case when GROUP_CONCAT(b.id) is null then ‘’ else GROUP_CONCAT(b.id) end as facility_id ,b.id as b_id from room_type as a
left join facility b on a.store_id = b.store_id and find_in_set(b.name,a.facility)
group by a.id
) c
ON s.id = c.id
set s.facility = c.facility_id

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值