MySQL column to row conversion tips (share)

MySQL column to row conversion tips (share)

Preface:

Because many business tables use design patterns that violate the first paradigm due to historical or performance reasons. That is, multiple attribute values ​​are stored in the same column (see the table below for the specific structure).

In this mode, applications often need to split the column based on the delimiter and obtain the result of column-to-row conversion.

Table data:

ID Value
1 tiny,small,big
2 small,medium
3 tiny,big

Expected results:

ID Value
1 tiny
1 small
1 big
2 small
2 medium
3 tiny
3 big

text:

#The table to be processed create table tbl_name (ID int, mSize varchar(100));
insert into tbl_name values ​​(1,'tiny,small,big');
insert into tbl_name values ​​(2,'small,medium');
insert into tbl_name values ​​(3,'tiny,big');

#AutoIncrement table for loop create table incre_table (AutoIncreID int);
insert into incre_table values ​​(1);
insert into incre_table values ​​(2);
insert into incre_table values ​​(3);
select a.ID,substring_index(substring_index(a.mSize,',',b.AutoIncreID),',',-1) 
from 
tbl_name a
join
incre_table b
on b.AutoIncreID <= (length(a.mSize) - length(replace(a.mSize,',',''))+1)
order by a.ID;

Principle analysis:

The most basic principle of this join is the Cartesian product. This is how the cycle is achieved.

The following is an analysis of the specific issues:

length(a.Size) - length(replace(a.mSize,',',''))+1 indicates the number of values ​​in the column after comma separation, referred to as n below.

Pseudo code of the join process:

Looping by ID

{

Judgment: whether i <= n

{

Get the data closest to the ith comma, that is, substring_index(substring_index(a.mSize,',',b.ID),',',-1)

i = i +1

}

ID = ID +1

}

Summarize:

The disadvantage of this approach is that we need a separate table (here increase_table) with the consecutive columns. And the maximum value of the continuous sequence must be greater than the number of values ​​that meet the segmentation requirements.

For example, if there is a row whose mSize has 100 comma-separated values, then our increase_table needs to have at least 100 consecutive rows.

Of course, there are also ready-made lists of consecutive numbers available in MySQL. For example, mysql.help_topic: help_topic_id has a total of 504 values, which can generally meet most needs.

After rewriting, it is as follows:

select a.ID,substring_index(substring_index(a.mSize,',',b.help_topic_id+1),',',-1) 
from 
tbl_name a
join
mysql.help_topic b
on b.help_topic_id < (length(a.mSize) - length(replace(a.mSize,',',''))+1)
order by a.ID;

The above MySQL column to row conversion tips (sharing) is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to convert a column of comma-separated values ​​into columns in MySQL
  • Detailed explanation of MySQL row locks when encountering composite primary keys and multi-column indexes
  • Mysql method to calculate the difference between two adjacent rows of a column
  • How to sort a row or column in mysql
  • MySQL column to row conversion, method of merging fields (must read)
  • MySQL column to row conversion and year-month grouping example
  • Detailed examples of converting rows to columns and columns to rows in MySQL
  • Implementation of dynamic conversion of mysql rows and columns (contingency table, cross table)
  • Database implementation of row and column conversion (mysql example)
  • How to convert rows to columns in MySQL

<<:  How to use selenium+testng to realize web automation in docker

>>:  js to achieve simple image drag effect

Recommend

Thinking about grid design of web pages

<br />Original address: http://andymao.com/a...

Detailed explanation of mysql replication tool based on python

Table of contents 1. Introduction Second practice...

HTML Tutorial: DOCTYPE Abbreviation

When writing HTML code, the first line should be ...

JavaScript deshaking and throttling examples

Table of contents Stabilization Throttling: Anti-...

CentOS7 uses rpm package to install mysql 5.7.18

illustrate This article was written on 2017-05-20...

How to use border-image to implement text bubble border sample code

During the development activity, I encountered a ...

Summary of various methods of implementing article dividing line styles with CSS

This article summarizes various ways to implement...

jQuery plugin to achieve carousel effect

A jQuery plugin every day - jQuery plugin to impl...

Detailed explanation of Vue project optimization and packaging

Table of contents Preface 1. Routing lazy loading...

How to deploy ElasticSearch in Docker

1. What is ElasticSearch? Elasticsearch is also d...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...

Linux common commands chmod to modify file permissions 777 and 754

The following command is often used: chmod 777 文件...

IIS and APACHE implement HTTP redirection to HTTPS

IIS7 Download the HTTP Rewrite module from Micros...

Tips for organizing strings in Linux

In Linux operations, we often replace and count s...