当前位置:网站首页>MySQL one line to many lines (split according to specific symbols)
MySQL one line to many lines (split according to specific symbols)
2022-07-19 04:36:00 【Qsh.】
Recently, there is a demand like this . The query estimated score is within a certain range
, For example, some people rate a person as 7 branch ,1 branch ,4 branch . Then query the score range in 5 be assigned to 10 Points of , If one of the scores is within the range, the data is valid . The data stored in the data table is as follows .
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pre_score` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test` (`id`, `pre_score`) VALUES (1, '5.5');
INSERT INTO `test` (`id`, `pre_score`) VALUES (2, '7,2');
INSERT INTO `test` (`id`, `pre_score`) VALUES (3, '1');
INSERT INTO `test` (`id`, `pre_score`) VALUES (4, '5.5');
INSERT INTO `test` (`id`, `pre_score`) VALUES (5, '2');
INSERT INTO `test` (`id`, `pre_score`) VALUES (6, '3');
INSERT INTO `test` (`id`, `pre_score`) VALUES (7, '4,8');
INSERT INTO `test` (`id`, `pre_score`) VALUES (8, '2,5');
INSERT INTO `test` (`id`, `pre_score`) VALUES (9, '5.5');
INSERT INTO `test` (`id`, `pre_score`) VALUES (10, '6');
Because there may be decimals in scoring , So at first, the query range is 5 - 10 branch , Cycle writing 5,6,7,8,9,10. And then use find_in_set You can't . So we can only consider using between. But the data table stores comma separated strings , So it's like how to split one piece of data into multiple pieces , And then use between Inquire about .
Use select * from test
that , Convert the data into the following , How to realize it :
Ordinary sql Realization ( Need to rely on mysql.help_topic surface )
SELECT
`id`,
substring_index( substring_index( t.pre_score, ',', b.help_topic_id + 1 ), ',',- 1 ) AS pre_score
FROM
test t
INNER JOIN mysql.help_topic b ON b.help_topic_id < ( LENGTH( t.pre_score ) - LENGTH( REPLACE ( t.pre_score, ',', '' )) + 1 )
WHERE
`pre_score` <> '';
mysql.help_topic Unauthorized handling method
mysql.help_topic
The function of is to SUBSTRING_INDEX Data from function ( That is, it is divided according to the delimiter ) Connect the data to make Cartesian product .
If mysql.help_topic
No authority , You can create a temporary table by yourself , Used to join the query with the table to be queried .
The field can be divided into several strings at most :
SELECT MAX(LENGTH(a.`name`) - LENGTH(REPLACE(a.`name`, ',', '' )) + 1) FROM `test` a;
Create a temporary table , And add data to the temporary table :
Be careful :
1. The temporary table must have a column from 0 perhaps 1 Self increasing data at the beginning
2. Temporary table name is optional , There can be only one... Field
3. The amount of data temporarily represented must be greater than MAX(LENGTH(a.name) - LENGTH(REPLACE(a.name, ',', '' )) + 1)
It's worth a lot
DROP TABLE IF EXISTS `tmp_help_topic`;
CREATE TABLE IF NOT EXISTS `tmp_help_topic` (
`help_topic_id` bigint(20) NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`help_topic_id`)
);
INSERT INTO `tmp_help_topic`() VALUES ();
INSERT INTO `tmp_help_topic`() VALUES ();
INSERT INTO `tmp_help_topic`() VALUES ();
INSERT INTO `tmp_help_topic`() VALUES ();
INSERT INTO `tmp_help_topic`() VALUES ();
INSERT INTO `tmp_help_topic`() VALUES ();
INSERT INTO `tmp_help_topic`() VALUES ();
INSERT INTO `tmp_help_topic`() VALUES ();
INSERT INTO `tmp_help_topic`() VALUES ();
INSERT INTO `tmp_help_topic`() VALUES ();
Inquire about :
SELECT
a.id,
a.num,
SUBSTRING_INDEX( SUBSTRING_INDEX( a.`name`, ',', b.help_topic_id ), ',',- 1 ) NAME
FROM
test a
JOIN tmp_help_topic b ON b.help_topic_id <= ( LENGTH( a.`name` ) - LENGTH( REPLACE ( a.`name`, ',', '' )) + 1 );
Function means
1.REPLACE function :
hold character string a,b,c,d Replace the comma inside with an empty string
SELECT REPLACE('a,b,c,d', ',', '');
-- Output : abcd
that :
# Get the number of commas
SELECT (LENGTH('a,b,c,d') - LENGTH(REPLACE('a,b,c,d', ',', '')));
# There are several elements after being separated by commas , After the partition here is a b c d, Namely 4 Elements
SELECT (LENGTH('a,b,c,d') - LENGTH(REPLACE('a,b,c,d', ',', '')) + 1);
2.SUBSTRING_INDEX function :
SUBSTRING_INDEX Is a string interceptor
SUBSTRING_INDEX(str, delim, count)
- str : Represents the string that needs to be split
- delim : Represents a separator , Split by a character
- count : When count Is a positive number , Take the first place n All characters before a separator ; When count It's a negative number , Take the penultimate n All characters after a delimiter .
for example :
SELECT SUBSTRING_INDEX('a*b*c*d', '*', 1); -- return : a
SELECT SUBSTRING_INDEX('a*b*c*d', '*', 2); -- return : a*b
SELECT SUBSTRING_INDEX('a*b*c*d', '*', 3); -- return : a*b*c
SELECT SUBSTRING_INDEX('a*b*c*d', '*', 4); -- return : a*b*c*d
SELECT SUBSTRING_INDEX('a*b*c*d', '*', -1); -- return : d
SELECT SUBSTRING_INDEX('a*b*c*d', '*', -2); -- return : c*d
SELECT SUBSTRING_INDEX('a*b*c*d', '*', -3); -- return : b*c*d
SELECT SUBSTRING_INDEX('a*b*c*d', '*', -4); -- return : a*b*c*d
that :
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('a*b*c*d', '*', 1), '*', -1); -- return : a
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('a*b*c*d', '*', 2), '*', -1); -- return : b
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('a*b*c*d', '*', 3), '*', -1); -- return : c
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('a*b*c*d', '*', 4), '*', -1); -- return : d
One line to many lines principle
go back to SQL
SELECT
`id`,
substring_index( substring_index( t.pre_score, ',', b.help_topic_id + 1 ), ',',- 1 ) AS pre_score
FROM
test t
INNER JOIN mysql.help_topic b ON b.help_topic_id < ( LENGTH( t.pre_score ) - LENGTH( REPLACE ( t.pre_score, ',', '' )) + 1 )
WHERE
`pre_score` <> '';
SUBSTRING_INDEX(SUBSTRING_INDEX(a.name, ',', b.help_topic_id), ',',-1 )
Is to gettmp_help_topic
Tabular help_topic_id The value of the field is used aspre_score
The number of substrings of the field- Used
join
Will put the fieldpre_score
It is divided into( LENGTH( t.pre_score ) - LENGTH( REPLACE ( t.pre_score, ',', '' )) + 1 )
That's ok , And the fields in each row are justpre_score
Field help_topic_id Substring
Final implementation requirements
SELECT
*
FROM
(
SELECT
`id`,
substring_index( substring_index( t.pre_score, ',', b.help_topic_id + 1 ), ',',- 1 ) AS pre_score
FROM
test t
INNER JOIN mysql.help_topic b ON b.help_topic_id < ( LENGTH( t.pre_score ) - LENGTH( REPLACE ( t.pre_score, ',', '' )) + 1 )
WHERE
`pre_score` <> ''
) a
WHERE
pre_score BETWEEN 5
AND 10
Reference resources :MySql One line becomes many lines ( Divide according to specific symbols )
边栏推荐
猜你喜欢
ospf防环
LeetCode之最大正方形(暴力求解和动态规划求解)
使用小丸工具箱进行极限视频压缩
微信小程序之项目结构
Data types of basic knowledge of C language
异或和加密方式的解密的复现
使用__slots__和__dict__来节省空间(简直就是质的飞越,LeetCode亲测有效)
51 single chip microcomputer to find out the input mode
The author of surging issued the pressure test results
Virtual lab basic experiment tutorial -7 Polarization (3)
随机推荐
The author of surging issued the pressure test results
51 single chip microcomputer to find out the input mode
Hello World driver
If by frame package name modifier
Avplayer adds playback progress monitoring
北斗时钟服务器(NTP服务器)让高考时间更加精准
对于每一个 Provider 实例都会维护多个连接
On the third day of security, iptables prevents nmap scanning and binlog
[Unity] Input.gettouch[index]的index
As a result, all system modifications of the company will be upgraded
删除文件 unable to find or create trash dirctory
64. Minimum path sum: given an M x n grid containing non negative integers, please find a path from the upper left corner to the lower right corner, so that the sum of the numbers on the path is the m
Wkwebview white screen
JS模态框
Architecture and application of secure multiparty computing
Autojs learning - map finding data generation
tf.AUTO_REUSE的作用
深度学习中的标量、向量、矩阵和张量的区别
ospf防环
VB. Net plug-in development - extract files