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

How to install and connect Navicat in MySQL 8.0.20 and what to pay attention to

Things to note 1. First, you need to create a my....

Two ways to reset the root password of MySQL database using lnmp

The first method: Use Junge's one-click scrip...

Common scenarios and avoidance methods for index failure in MySQL

Preface I have read many similar articles before,...

Implementation of Docker deployment of ElasticSearch and ElasticSearch-Head

This article mainly explains how to deploy Elasti...

Use Docker Compose to quickly deploy ELK (tested and effective)

Table of contents 1. Overview 1.1 Definition 1.2 ...

Docker modifies the configuration information of an unstarted container

When I first used docker, I didn't use docker...

Basic usage tutorial of IPTABLES firewall in LINUX

Preface For production VPS with public IP, only t...

MySQL time types and modes details

Table of contents 1. MySQL time type 2. Check the...

Several practical scenarios for implementing the replace function in MySQL

REPLACE Syntax REPLACE(String,from_str,to_str) Th...

MySQL 5.7.18 Archive compressed version installation tutorial

This article shares the specific method of instal...

Docker images export and import operations

What if the basic images have been configured bef...

Vue+axios sample code for uploading pictures and recognizing faces

Table of contents Axios Request Qs processing dat...

JavaScript typing game

This article shares the specific code of JavaScri...