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
Table of contents Preface Query usage scenario ca...
In order to solve the problem mentioned last time...
This article example shares the specific code of ...
mysql accidentally deleted data Using the delete ...
Table of contents Add Configuration json configur...
This article shares with you the tutorial of inst...
This article shares the specific code of jQuery t...
I believe many programmers are familiar with MySQ...
Drop-down menus are also very common in real life...
"Grand" are probably the two words that ...
Find the problem Today I am going to study the to...
This article example shares the specific code of ...
This article mainly introduces the sql script fun...
Table of contents Placeholder replacement Console...
Related articles: Install Docker using yum under ...