Preface Sometimes you come across business tables that do not follow the first normal form design pattern. That is, multiple attribute values are stored in one column. As shown in the following table
There are generally two common requirements (see the end of the article for test data) 1. Get all non-repeating values, such as
The SQL is as follows: select distinct(substring_index(substring_index(a.col,',',b.help_topic_id+1),',',-1)) from (select group_concat(distinct `value`) as col from `row_to_col`) as a join mysql.help_topic as b on b.help_topic_id < (char_length(a.col) - char_length(replace(a.col,',',''))+1) 2. Display each value and its corresponding primary key, such as
The SQL is as follows: select a.pk,substring_index(substring_index(a.col,',',b.help_topic_id+1),',',-1) from (select `value` as col,pk from `row_to_col`) as a join mysql.help_topic as b on b.help_topic_id < (char_length(a.col) - char_length(replace(a.col,',',''))+1) Implementation ideas: Requirement 1: 1. Use the group_concat function to concatenate the values of the value column into a comma-separated string, and then use the substring_index function to intercept the string Notice: Requirement 2: The idea is basically the same as Requirement 1, except that the final query is different Functions involved: length: Returns the number of bytes occupied by the string, which is the length of the calculated field. A Chinese character or Chinese symbol is counted as three characters, and a number, letter or English symbol is counted as one character. Test data: DROP TABLE IF EXISTS `row_to_col`; CREATE TABLE `row_to_col` ( `pk` int(11) NOT NULL AUTO_INCREMENT, `value` varchar(255) DEFAULT NULL, PRIMARY KEY (`pk`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; INSERT INTO `row_to_col` VALUES ('1', 'ET,AT'); INSERT INTO `row_to_col` VALUES ('2', 'AT,BT'); INSERT INTO `row_to_col` VALUES ('3', 'AT,DT'); INSERT INTO `row_to_col` VALUES ('4', 'DT,CT,AT'); refer to: This is the end of this article about how to convert a column of comma-separated values into rows and columns in MySQL. For more information about MYSQL comma-separated values into rows and columns, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed tutorial of pycharm and ssh remote access server docker
>>: Implementation of select multiple data loading optimization in Element
NC's full name is Netcat (Network Knife), and...
Recently I've been working on a framework tha...
Article Structure 1. Preparation 2. Install Java ...
Table of contents 1. Why do we need vue3? 2. Adva...
This article example shares the specific code of ...
The most common, most commonly used and most gener...
Create a table create table order(id varchar(10),...
Data sorting asc, desc 1. Single field sorting or...
Recently, I used vuethink in my project, which in...
The <marquee> tag is a tag that appears in ...
This article uses examples to illustrate the comm...
Separation of static and dynamic Dynamic requests...
Table of contents Steps to create TCP in Linux Se...
Concept introduction: We know that the redo log i...
Now that we have finished the transform course, l...