mysql的单列多值存储实例详解


Posted in MySQL onApril 05, 2022

本文主要研究一下mysql如何用一个列来存储多个值

实例

用bit类型

  • 建表及数据准备
-- 这里定义了bit(3),表示有3位,第一位1,第二位2,第三位4
create table t_bit_demo(
   id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   multi_value bit(3) not null default 0
);

-- 这里插入了1,2,4的组合值
insert into t_bit_demo(multi_value) values(b'000');
insert into t_bit_demo(multi_value) values(b'001');
insert into t_bit_demo(multi_value) values(b'010');
insert into t_bit_demo(multi_value) values(b'011');
insert into t_bit_demo(multi_value) values(b'100');
insert into t_bit_demo(multi_value) values(b'101');
insert into t_bit_demo(multi_value) values(b'110');
insert into t_bit_demo(multi_value) values(b'111');

-- 这里直接插入int值也可以,比如5相当于101
-- insert into t_bit_demo(multi_value) values(5);

SELECT multi_value+0, BIN(multi_value) FROM t_bit_demo;
+---------------+------------------+
| multi_value+0 | BIN(multi_value) |
+---------------+------------------+
| 0             | 0                |
| 1             | 1                |
| 2             | 10               |
| 3             | 11               |
| 4             | 100              |
| 5             | 101              |
| 6             | 110              |
| 7             | 111              |
+---------------+------------------+
  • 位运算查询
-- 查询第二位有值的数据
select multi_value+0,BIN(multi_value) from t_bit_demo where multi_value & 2
+---------------+------------------+
| multi_value+0 | BIN(multi_value) |
+---------------+------------------+
| 2             | 10               |
| 3             | 11               |
| 6             | 110              |
| 7             | 111              |
+---------------+------------------+

-- 查询第三位有值的数据
select multi_value+0,BIN(multi_value) from t_bit_demo where multi_value & 4
+---------------+------------------+
| multi_value+0 | BIN(multi_value) |
+---------------+------------------+
| 4             | 100              |
| 5             | 101              |
| 6             | 110              |
| 7             | 111              |
+---------------+------------------+

-- 查询只有第三位有值的数据
select multi_value+0,BIN(multi_value) from t_bit_demo where multi_value = 4
select multi_value+0,BIN(multi_value) from t_bit_demo where multi_value = 4
+---------------+------------------+
| multi_value+0 | BIN(multi_value) |
+---------------+------------------+
| 4             | 100              |
+---------------+------------------+
  • 更新
select id,multi_value+0,BIN(multi_value) from t_bit_demo
+----+---------------+------------------+
| id | multi_value+0 | BIN(multi_value) |
+----+---------------+------------------+
| 1  | 0             | 0                |
| 2  | 1             | 1                |
| 3  | 2             | 10               |
| 4  | 3             | 11               |
| 5  | 4             | 100              |
| 6  | 5             | 101              |
| 7  | 6             | 110              |
| 8  | 7             | 111              |
+----+---------------+------------------+

-- 将id为7的值移除第二个枚举
update t_bit_demo set multi_value = b'100' where id=7
select id,multi_value+0,BIN(multi_value) from t_bit_demo where id=7
+----+---------------+------------------+
| id | multi_value+0 | BIN(multi_value) |
+----+---------------+------------------+
| 7  | 4             | 100              |
+----+---------------+------------------+

用int/bigint类型

  • 建表及数据准备
create table t_bigint_demo(
   id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   multi_value bigint not null default 0
);

-- 假设这里定义了1,2,4三个枚举值
insert into t_bigint_demo(multi_value) values(0);
insert into t_bigint_demo(multi_value) values(1);
insert into t_bigint_demo(multi_value) values(2);
insert into t_bigint_demo(multi_value) values(3);
insert into t_bigint_demo(multi_value) values(4);
insert into t_bigint_demo(multi_value) values(5);
insert into t_bigint_demo(multi_value) values(6);
insert into t_bigint_demo(multi_value) values(7);

select multi_value from t_bigint_demo
+-------------+
| multi_value |
+-------------+
| 0           |
| 1           |
| 2           |
| 3           |
| 4           |
| 5           |
| 6           |
| 7           |
+-------------+
  • 查询
-- 查询包含第二个枚举的数据
select multi_value,BIN(multi_value) from t_bigint_demo where multi_value & 2
+-------------+------------------+
| multi_value | BIN(multi_value) |
+-------------+------------------+
| 2           | 10               |
| 3           | 11               |
| 6           | 110              |
| 7           | 111              |
+-------------+------------------+

-- 查询包含第三个枚举的数据
select multi_value,BIN(multi_value) from t_bigint_demo where multi_value & 4
+-------------+------------------+
| multi_value | BIN(multi_value) |
+-------------+------------------+
| 4           | 100              |
| 5           | 101              |
| 6           | 110              |
| 7           | 111              |
+-------------+------------------+

-- 查询值为第三个枚举的数据
select multi_value,BIN(multi_value) from t_bigint_demo where multi_value =4
+-------------+------------------+
| multi_value | BIN(multi_value) |
+-------------+------------------+
| 4           | 100              |
+-------------+------------------+
  • 更新
select id,multi_value,BIN(multi_value) from t_bigint_demo
+----+-------------+------------------+
| id | multi_value | BIN(multi_value) |
+----+-------------+------------------+
| 1  | 0           | 0                |
| 2  | 1           | 1                |
| 3  | 2           | 10               |
| 4  | 3           | 11               |
| 5  | 4           | 100              |
| 6  | 5           | 101              |
| 7  | 6           | 110              |
| 8  | 7           | 111              |
+----+-------------+------------------+

-- 将id为7的值移除第二个枚举
update t_bigint_demo set multi_value = b'100' where id=7
select id,multi_value,BIN(multi_value) from t_bigint_demo where id=7
+----+-------------+------------------+
| id | multi_value | BIN(multi_value) |
+----+-------------+------------------+
| 7  | 4           | 100              |
+----+-------------+------------------+

用varchar类型

  • 建表及数据准备
create table t_varchar_demo(
   id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   multi_value varchar(255) not null default ''
);

-- 假设这里定义了1,2,4三个枚举值
insert into t_varchar_demo(multi_value) values('1');
insert into t_varchar_demo(multi_value) values('2');
insert into t_varchar_demo(multi_value) values('1,2');
insert into t_varchar_demo(multi_value) values('4');
insert into t_varchar_demo(multi_value) values('1,4');
insert into t_varchar_demo(multi_value) values('2,4');
insert into t_varchar_demo(multi_value) values('1,2,4');

select multi_value from t_varchar_demo
+-------------+
| multi_value |
+-------------+
| 1           |
| 2           |
| 1,2         |
| 4           |
| 1,4         |
| 2,4         |
| 1,2,4       |
+-------------+
  • 查询
-- 查询包含第二个枚举的数据
select multi_value from t_varchar_demo where find_in_set('2',multi_value)
+-------------+
| multi_value |
+-------------+
| 2           |
| 1,2         |
| 2,4         |
| 1,2,4       |
+-------------+

-- 查询包含第三个枚举的数据
select multi_value from t_varchar_demo where find_in_set('4',multi_value)
+-------------+
| multi_value |
+-------------+
| 4           |
| 1,4         |
| 2,4         |
| 1,2,4       |
+-------------+

-- 查询只有第三个枚举的数据
select multi_value from t_varchar_demo where multi_value = '4'
+-------------+
| multi_value |
+-------------+
| 4           |
+-------------+
  • 更新
select * from t_varchar_demo
+----+-------------+
| id | multi_value |
+----+-------------+
| 1  | 1           |
| 2  | 2           |
| 3  | 1,2         |
| 4  | 4           |
| 5  | 1,4         |
| 6  | 2,4         |
| 7  | 1,2,4       |
+----+-------------+

-- 将id为7的值移除第二个枚举
update t_varchar_demo set multi_value = '1,4' where id=7
select * from t_varchar_demo where id=7
+----+-------------+
| id | multi_value |
+----+-------------+
| 7  | 1,4         |
+----+-------------+

用set类型

  • 建表及数据准备
create table t_set_demo(
   id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   multi_value set('1','2','4') not null default ''
);

insert into t_set_demo(multi_value) values('');
insert into t_set_demo(multi_value) values('1');
insert into t_set_demo(multi_value) values('2');
insert into t_set_demo(multi_value) values('1,2');
insert into t_set_demo(multi_value) values('4');
insert into t_set_demo(multi_value) values('1,4');
insert into t_set_demo(multi_value) values('2,4');
insert into t_set_demo(multi_value) values('1,2,4');
  • 查询
-- 查询包含第二个枚举的数据,可以用位运算也可以用find_in_set
select multi_value from t_set_demo where multi_value&2
select multi_value from t_set_demo where find_in_set('2',multi_value)
+-------------+
| multi_value |
+-------------+
| 2           |
| 1,2         |
| 2,4         |
| 1,2,4       |
+-------------+

-- 查询包含第三个枚举的数据,可以用位运算也可以用find_in_set
select multi_value from t_set_demo where multi_value&4
select multi_value from t_set_demo where find_in_set('4',multi_value)
+-------------+
| multi_value |
+-------------+
| 4           |
| 1,4         |
| 2,4         |
| 1,2,4       |
+-------------+

-- 查询值为第三个枚举的数据
select multi_value from t_set_demo where multi_value='4'
+-------------+
| multi_value |
+-------------+
| 4           |
+-------------+
  • 更新
select * from t_set_demo
+----+-------------+
| id | multi_value |
+----+-------------+
| 1  |             |
| 2  | 1           |
| 3  | 2           |
| 4  | 1,2         |
| 5  | 4           |
| 6  | 1,4         |
| 7  | 2,4         |
| 8  | 1,2,4       |
+----+-------------+

-- 将id为7的值移除第二个枚举
update t_set_demo set multi_value = '1,4' where id=7
select * from t_set_demo where id=7
select * from t_set_demo where id=7
+----+-------------+
| id | multi_value |
+----+-------------+
| 7  | 1,4         |
+----+-------------+

小结

mysql用单列存储多值通常用于一对多的反范式处理,具体可以用bit、int/bigint、varchar、set类型来实现,缺点是不支持索引。

doc

到此这篇关于mysql单列多值存储的文章就介绍到这了,更多相关mysql单列多值存储内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

MySQL 相关文章推荐
MySQL入门命令之函数-单行函数-流程控制函数
Apr 05 MySQL
MySQL root密码的重置方法
Apr 21 MySQL
MySQL分库分表与分区的入门指南
Apr 22 MySQL
JDBC连接的六步实例代码(与mysql连接)
May 12 MySQL
为什么MySQL选择Repeatable Read作为默认隔离级别
Jul 26 MySQL
Mysql8.0递归查询的简单用法示例
Aug 04 MySQL
mysql中int(3)和int(10)的数值范围是否相同
Oct 16 MySQL
如何创建一个创建MySQL数据库中的datetime类型
Mar 21 MySQL
MySQL视图概念以及相关应用
Apr 19 MySQL
讲解MySQL增删改操作
May 06 MySQL
MySQL解决Navicat设置默认字符串时的报错问题
Jun 16 MySQL
MySQL 原理与优化之Limit 查询优化
Aug 14 MySQL
详细聊一聊mysql的树形结构存储以及查询
mysql查询结果实现多列拼接查询
Apr 03 #MySQL
mysql使用instr达到in(字符串)的效果
数据分析数据库ClickHouse在大数据领域应用实践
Apr 03 #MySQL
一文了解MYSQL三大范式和表约束
MYSQL优化之数据表碎片整理详解
Innodb存储引擎中的后台线程详解
Apr 03 #MySQL
You might like
360通用php防护代码(使用操作详解)
2013/06/18 PHP
php中sprintf与printf函数用法区别解析
2014/02/17 PHP
PHP写日志的实现方法
2014/11/05 PHP
PHP示例演示发送邮件给某个邮箱
2019/04/03 PHP
用js计算页面执行时间的函数
2006/12/07 Javascript
js两行代码按指定格式输出日期时间
2011/10/21 Javascript
利用JS进行图片的切换即特效展示图片
2013/12/03 Javascript
在JavaScript应用中实现延迟加载的方法
2015/06/25 Javascript
vue利用better-scroll实现轮播图与页面滚动详解
2017/10/20 Javascript
js中的 || 与 && 运算符详解
2018/05/24 Javascript
[04:27]DOTA2官方论坛水友赛集锦
2013/09/16 DOTA
Python爬虫基础之XPath语法与lxml库的用法详解
2018/09/13 Python
使用pandas把某一列的字符值转换为数字的实例
2019/01/29 Python
python实现杨氏矩阵查找
2019/03/02 Python
Python for循环搭配else常见问题解决
2020/02/11 Python
Python 使用 environs 库定义环境变量的方法
2020/02/25 Python
详解pandas获取Dataframe元素值的几种方法
2020/06/14 Python
Pytorch学习之torch用法----比较操作(Comparison Ops)
2020/06/28 Python
python,Java,JavaScript实现indexOf
2020/09/09 Python
实习评语
2013/12/16 职场文书
综合办公室个人的自我评价
2013/12/22 职场文书
留学生如何写好自荐信
2013/12/27 职场文书
大学生水果店创业计划书
2014/01/28 职场文书
检举信的格式及范文
2014/04/04 职场文书
模具专业求职信
2014/06/26 职场文书
教师暑期培训感言
2014/08/15 职场文书
乡镇挂职心得体会
2014/09/04 职场文书
党的群众路线学习笔记
2014/11/06 职场文书
语文教师个人工作总结
2015/02/06 职场文书
淘宝文案策划岗位职责
2015/04/14 职场文书
2015年办税服务厅工作总结
2015/07/23 职场文书
遗嘱格式范本
2015/08/07 职场文书
公安纪律作风整顿心得体会
2016/01/23 职场文书
pytorch损失反向传播后梯度为none的问题
2021/05/12 Python
react中的DOM操作实现
2021/06/30 Javascript
CentOS7安装GlusterFS集群以及相关配置
2022/04/12 Servers