当前位置:网站首页>数据库单字段存储多个标签(位移操作)
数据库单字段存储多个标签(位移操作)
2022-08-01 20:22:00 【Q z1997】
在上周的开发中看到了这样的一个问题 一个员工有多个资质
如: 资质 1=监理,2=安全员,4=项目经理,8=技术员,16=特种作业人员,32=劳务人员 (是2的倍数 就是二进制的10进制的展示 )
李白 1=监理 4=项目经理
一般我们的解决方式有以下两种
- 在员工表这里扩展字段
- 配置资质表 和 一个关系表 然后链表查询
方案1 没有使用的原因 是因为这个资质可能会频繁的增加 修改 就要频繁的改表结构了 数据量一大 改表结构就不是一个好的操作
方法2 这里没有使用的原因 就是这个用工查询本身就关联表过多了 要不就要2次请求数据库
演示
这里写了一个Demo 提供一下思路
首先准备一个员工表
CREATE TABLE `emp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`age` int(11) DEFAULT NULL,
`deptid` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`empno` int(11) DEFAULT NULL,
`del_flag` int(11) DEFAULT '0',
`record_version` int(255) DEFAULT '1',
`qualification` int(11) NOT NULL COMMENT '1=监理2=安全员4=项目经理8=技术员16=特种作业人员32=劳务人员',
PRIMARY KEY (`id`),
KEY `i` (`age`,`deptid`,`name`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4;
使用这个qualification
字段来表示 这个员工的资质
插入
比如 上述例子 李白 拥有 1=监理 4=项目经理 这两个资质 插入sql 如下:
-- 在Java 中资质可以是多个 使用 mybatis的 foreach
insert into emp (name,qualification) values ('李白', 1<<0 | 1<<4)
查询
数据:
-- 1=监理,2=安全员,4=项目经理,8=技术员,16=特种作业人员
-- 在Java 中资质可以是多个 使用 mybatis的 foreach
select name,
(attrbute & (1<<0) !=0) as 监理,
(attrbute & (1<<1) !=0) as 安全员,
(attrbute & (1<<2) !=0) as 项目经理,
(attrbute & (1<<3) !=0) as 技术员,
(attrbute & (1<<4) !=0) as 特种作业人员,
(attrbute & (1<<5) !=0) as 劳务人员
from emp
where
attrbute != 0
结果
统计
select
sum((qualification & (1<<0) !=0)) as 监理,
sum((qualification & (1<<1) !=0)) as 安全员,
sum((qualification & (1<<2) !=0)) as 项目经理,
sum((qualification & (1<<3) !=0)) as 技术员,
sum((qualification & (1<<4) !=0)) as 特种作业人员,
sum((qualification & (1<<5) !=0)) as 劳务人员
from emp
where
qualification != 0
条件查询
select *
from emp
where
qualification != 0
# 仅查询监理
and qualification & 1 !=0
--------------------------------------------
select *
from emp
where
qualification != 0
# 仅查询监理 并且是 项目经理
and qualification & ( 1<<0 | 1<<4) = (1<<0 | 1<<4)
---------------------------------------
select *
from emp
where
qualification != 0
# 仅查询监理 或 项目经理
and qualification & ( 1<<0 | 1<<4) != 0
修改 减少标签
-- 在Java 中资质可以是多个 使用 mybatis的 foreach
update emp set qualification = qualification ^ (1<<4) where id = 19
查看结果
select id , name,(qualification & (1<<0) !=0) as 监理,
(qualification & (1<<1) !=0) as 安全员,
(qualification & (1<<2) !=0) as 项目经理,
(qualification & (1<<3) !=0) as 技术员,
(qualification & (1<<4) !=0) as 特种作业人员,
(qualification & (1<<5) !=0) as 劳务人员 from emp where id = 19
修改 增加标签
-- 在Java 中资质可以是多个 使用 mybatis的 foreach
update emp set qualification = qualification | (1<<4) where id = 19
查看结果
select id , name,(qualification & (1<<0) !=0) as 监理,
(qualification & (1<<1) !=0) as 安全员,
(qualification & (1<<2) !=0) as 项目经理,
(qualification & (1<<3) !=0) as 技术员,
(qualification & (1<<4) !=0) as 特种作业人员,
(qualification & (1<<5) !=0) as 劳务人员 from emp where id = 19
边栏推荐
- myid file is missing
- 【七夕特别篇】七夕已至,让爱闪耀
- LTE time domain and frequency domain resources
- 我的驾照考试笔记(1)
- 18. Distributed configuration center nacos
- 互联网大厂研发流程
- Use WeChat official account to send information to designated WeChat users
- 多线程之生产者与消费者
- 57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom
- 58: Chapter 5: Develop admin management services: 11: Develop [admin face login, interface]; (not measured) (using Ali AI face recognition) (demonstrated, using RestTemplate to implement interface cal
猜你喜欢
WhatsApp群发实战分享——WhatsApp Business API账号
【个人作品】无线网络图传模块
虚拟机的IP地址自动变为127.0.0.1
Buttons with good user experience should not have hover state on mobile phones
部署zabbix
[Energy Conservation Institute] Ankerui Food and Beverage Fume Monitoring Cloud Platform Helps Fight Air Pollution
【节能学院】智能操控装置在高压开关柜的应用
智能硬件开发怎么做?机智云全套自助式开发工具助力高效开发
Does LabVIEW really close the COM port using VISA Close?
LTE时域、频域资源
随机推荐
datax - 艰难debug路
Buttons with good user experience should not have hover state on mobile phones
PROE/Croe如何编辑已完成的草图,让其再次进入草绘状态
大神经验:软件测试的自我发展规划
Little data on how to learn?Jida latest small learning data review, 26 PDF page covers the 269 - page document small data learning theory, method and application are expounded
漏刻有时文档系统之XE培训系统二次开发配置手册
因斯布鲁克大学团队量子计算硬件突破了二进制
】 【 nn. The Parameter () to generate and why do you want to initialize
[Energy Conservation Institute] Comparative analysis of smart small busbar and column head cabinet solutions in data room
Remove 360's detection and modification of the default browser
SIPp installation and use
内网穿透 lanproxy部署
环境变量,进程地址空间
【kali-信息收集】(1.5)系统指纹识别:Nmap、p0f
The Internet giant development process
【luogu P1912】诗人小G(二分栈)(决策单调性优化DP)
57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom
58:第五章:开发admin管理服务:11:开发【管理员人脸登录,接口】;(未实测)(使用了阿里AI人脸识别)(演示了,使用RestTemplate实现接口调用接口;)
Multithreaded producers and consumers
58: Chapter 5: Develop admin management services: 11: Develop [admin face login, interface]; (not measured) (using Ali AI face recognition) (demonstrated, using RestTemplate to implement interface cal