系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 数据库 > Mysql > 详细页面

mysql中bitmap的简单运用

时间:2019-12-02来源:系统城作者:电脑系统城

bitmap就是在一个二进制的数据中,每一个位代表一定的含义,这样最终只需要存一个整型数据,就可以解释出多个含义.
业务中有一个字段专门用来存储用户对某些功能的开启和关闭,如果是传统的思维,肯定是建一个字段来存0代表关闭,1代表开启,那么如果功能很多或者需要加功能开关,就需要不停的创建字段.
使用bitmap的思路就只需要一个字段就可以了,建一个entuserstatus字段,该字段的二进制表示中,从右到做数,从1开始数.比如第19位代表是否开始归档,那么就直接操作这一位的0和1就可以表示该用户是否开启归档功能.


email表的第19位,作为归档开启的位,1是开启 0是关闭;262144代表是第19位为1的十进制数
查询开启的
select email,enterpriseId from email where entuserstatus & 262144=262144;
开启归档
update email set entuserstatus = entuserstatus|262144 where id=670602 limit 1
关闭归档
update email set entuserstatus = entuserstatus^262144 where id=670602 limit 1

另一种形式
查询开启归档的
select id,email,enterpriseId,entuserstatus from email where entuserstatus>>18 & 1=1;
开启归档
update email set entuserstatus = entuserstatus|(1<<18)  where id=670602 limit 1
关闭归档
update email set entuserstatus = entuserstatus^(1<<18)  where id=670602 limit 1

 

异或(^)运算
异或运算通俗地讲就是一句话
同为假,异为真
所以它是这样的算法:
0&0=0,0&1=1,1&0=1,1&1=0

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载