mysql 实现将数据拆分一列变成多行
一 表结构:
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`brands` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'a,b,c', 'liu,jian,fu', '23');
INSERT INTO `student` VALUES ('2', 'e,f', 'jian,kk', '34');
结果图:
需要对name,brands字段的数据,通过逗号进行分割,第一行应该是:1*3*3=9;第二行应该是:1*2*2=4,总共因该有13条数据
二 编写语句:
select c.id ,brands,name from (
SELECT a.id as id,
substring_index( substring_index( a.name, ',', b.help_topic_id + 1 ), ',',- 1 ) name
FROM student a
JOIN mysql.help_topic b ON b.help_topic_id < (length( a.name ) - length( REPLACE ( a.name, ',', '' ) ) + 1)
ORDER BY a.id ) as c ,
(
SELECT a.id as id,
substring_index( substring_index( a.brands, ',', b.help_topic_id + 1 ), ',',- 1 ) brands
FROM student a
JOIN mysql.help_topic b ON b.help_topic_id < (length( a.brands ) - length( REPLACE ( a.brands, ',', '' ) ) + 1)
ORDER BY a.id
) as d where c.id=d.id group by id,brands,name
结果:
相关文章
- 【阿里年薪百万数据库面试】MySQL会丢数据吗?
- Vc数据库编程基础MySql数据库的表增删改查数据
- MySQL存储引擎选型
- mysql 使用存储过程批量插数据
- MySql中把一个表的数据插入到另一个表中的实现代码
- mysql存储过程3-1 实现每天定时从A表按天同步到B表(含建表,同步数据,删除旧数据)
- mysql中将多行数据合并成一行显示
- mysql时间截取函数和实现数据累加
- MySQL数据同步ES的常用思路和方法
- MySQL 主从同步(3)-percona-toolkit工具(数据一致性监测、延迟监控)使用梳理
- 报错:对象必须实现 IConvertible;以分隔符进行分割链接concat_ws的使用方法;mysql数据类型转换cast,convert
- mysql 查询某个日期时间段,每天同一时间段的数据
- mysql数据向Redis快速导入
- 2023-03-03 mysql-join类别-分析
- 2023-01-04 mysql列存储引擎-读取data数据-分析
- Flink Mysql CDC结合Doris flink connector实现数据实时入库
- 第107课: Spark Streaming电商广告点击综合案例底层数据层的建模和编码实现(基于MySQL)
- Hibernate原生SQL映射MySQL的CHAR(n)类型到String时出错
- APNS MySQL Tables
- dedeCMS /data/mysql_error_trace.php DB error raised PHP Code Injection Via /include/dedesql.class.php Log FIle Without Access Validation
- MySQL之查询指定时间的数据
- MySQL:主从复制跳过错误
- Unity3D调用MySQL执行数据的增删改查
- MYSQL分页查询时没有用ORDER BY出现数据重复的问题
- 在linux下实现mysql自动备份数据
- centos7安装mysql 8
- mysql数据导入