Home  >  Article  >  Database  >  通过sql语句将blob里的char取出来转成数字保存在其它字段

通过sql语句将blob里的char取出来转成数字保存在其它字段

WBOY
WBOYOriginal
2016-06-07 18:05:191158browse

现在需要将blob里地17、18、19三个字段里的数据作为数字保存在blob外新增的三个字段Gem1 Gem2 Gem3上。

这个需求是有个表结构,本身设计为

但现在需要将blob里地17、18、19三个字段里的数据作为数字保存在blob外新增的三个字段Gem1 Gem2 Gem3上。

通过下面的sql语句可以做到:
1、增加三个字段:
代码如下:
alter table EquipmentInfo add Gem1 TINYINT UNSIGNED default 0;
alter table EquipmentInfo add Gem2 TINYINT UNSIGNED default 0;
alter table EquipmentInfo add Gem3 TINYINT UNSIGNED default 0;

2、使用下面的命令把blob里的数据拷贝出来
update EquipmentInfo set Gem1=conv(substr(HEX(EquipmentBlob),17,2),16,10),Gem2=conv(substr(HEX(EquipmentBlob),19,2),16,10),Gem3=conv(substr(HEX(EquipmentBlob),21,2),16,10);
说明:
HEX(EquipmentBlob) 将EquipmentBlob转成16进制的字符串
substr(str,beginIdx,num) 将str从beginIdx开始的字符串截取出来,截取长度为num
conv(N,from_base,to_base) N是要转换的数据,from_base是原进制,to_base是目标进制。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn