MYSQL A question about using character functions to filter data

MYSQL A question about using character functions to filter data

Problem description:

structure:

test has two fields,
They are col1 and col2, both are character fields.
The contents are three numbers separated by , and they correspond one to one.

For example, col1 content is: 26,59,6
col2 content is: 1502.5,1690,2276.77
The one-to-one correspondence is that the value of 26 is 1502.5, 59 is 1690, and 6 corresponds to 2276.77


Search criteria:

Select an id, such as 59, and then enter a number, such as: 2000
Then we search for records with id=59 in col1, and then search for records with col2 less than 2000, that is, 1690<2000

Example:

If there are the following three records, search for records with id 59 and value less than 2000:

26,59,6 | 1502.5,1690,2276.77
59,33,6 | 3502.1,1020,2276.77
22,8,59 | 1332.6,2900,1520.77

The three records with id 59 are searched, and then the second search condition is determined to be (that is, the numbers at the corresponding id positions are compared):

1690<2000
3502.1>2000
1520.77<2000

drop table test; 
create table test ( col1 varchar(100), col2 varchar(100)); 
insert test select 
'26,59,6', '1502.5,1690,2276.77' union all select 
'59,33,6', '3502.1,1020,2276.77' union all select 
'22,8,59', '1332.6,2900,1520.77'; 
select col1,col2 
from (select *,find_in_set('59',col1) as rn from test) k 
where substring_index(concat(',',substring_index(col2,',',rn)),',',-1) 
 <'2000'; 

+---------+---------------------+

| col1 | col2 |

+---------+---------------------+

| 26,59,6 | 1502.5,1690,2276.77 |

| 22,8,59 | 1332.6,2900,1520.77 |

+---------+---------------------+

<<:  js uses cookies to remember user page operations

>>:  How to bind Docker container to external IP and port

Recommend

Problems installing TensorRT in docker container

Uninstall the installed version on Ubuntu: sudo a...

Examples of using the or statement in MySQL

1. The use of or syntax in MySQL, and the points ...

HTML Basics: The basic structure of HTML

The basic structure of HTML hypertext documents is...

Examples of correct use of maps in WeChat mini programs

Table of contents Preface 1. Preparation 2. Actua...

jenkins+gitlab+nginx deployment of front-end application

Table of contents Related dependency installation...

SQL method for calculating timestamp difference

SQL method for calculating timestamp difference O...

Summary of Linux nc command

NC's full name is Netcat (Network Knife), and...

2 reasons why html-css tag style setting does not work

1 CSS style without semicolon ";" 2 Tags...

Use of Linux cal command

1. Command Introduction The cal (calendar) comman...

Linux system (Centos6.5 and above) installation jdk tutorial analysis

Article Structure 1. Preparation 2. Install Java ...

Teach you 10 ways to center horizontally and vertically in CSS (summary)

A must-have for interviews, you will definitely u...

Use Navicate to connect to MySQL on Alibaba Cloud Server

1. First enter the server's mysql to modify p...

Detailed analysis of MySQL optimization of like and = performance

introduction Most people who have used databases ...