Two solutions to the problem of MySQL in conditional statement only reading one piece of information

Two solutions to the problem of MySQL in conditional statement only reading one piece of information

Today, my colleague encountered a very strange problem when writing MYSQL query statements. When using MySQL multi-table query, a field in one table is used as the in query condition of another table, only one piece of information can be read, but it can be read normally if a number is used directly.

The SQL statement is as follows:

select a.id,a.title,b.idlist,b.aid from table a,table2 b where a.id in(b.idlist) and b.aid=2

The idlist field in table2 is of varchar type and stores multiple id information in table, i.e. 1, 1,2,3,4

The above statement can be used to query normally, but only one piece of information can be queried. However, the following statement can be used to read normally.

select a.id,a.title,b.idlist,b.aid from table a,table2 b where a.id in(1,2,3,4) and b.aid=2

This is a very strange problem. I thought it was a data type problem at first, but to save data in the format of 1,2,3,4 , I can only use character type. I tried many ways but couldn't solve the problem until I found the FIND_IN_SET() function in MYSQL. The following is a basic introduction to the FIND_IN_SET() function.

Syntax: FIND_IN_SET(str,strlist)

definition:

1. If the string str is in the string list strlist consisting of N subchains, the return value ranges from 1 to N.

2. A string list is a string consisting of a number of self-links separated by ',' symbols.

3. If the first argument is a constant string and the second is a typeSET column, the FIND_IN_SET() function is optimized to use bitwise calculations.

4. If str is not in strlist or strlist is an empty string, the return value is 0.

5. If any parameter is NULL, the return value is NULL. This function will not work properly if the first argument contains a comma (',').

strlist: A string linked by English commas "," for example: "a,b,c,d". The string is similar to the SET type value linked by commas.

Example: SELECT FIND_IN_SET('b','a,b,c,d'); //The return value is 2, which is the second value

OK, let's try it first and modify the original SQL statement to

select a.id,a.title,b.idlist,b.aid from table a,table2 b where FIND_IN_SET(a.id,b.idlist) and b.aid=2

Execute the modified statement, and it can finally be read normally. After analyzing the reason, it is ultimately due to the data type problem. When we directly in(b.idlist), the b.idlist read is a character type, and in only accepts numbers. Although both have "," they are actually completely different.

Well, the problem is solved. If you want to know more about how to use the FIND_IN_SET() function, you can read the related articles on the page.

You may also be interested in:
  • Analyze the sql statement efficiency optimization issues of Mysql table reading, writing, indexing and other operations
  • MySQL SQL statement to find duplicate data based on one or more fields
  • How to get the query time of MySQL SQL statement in PHP
  • Mysql query the most recent record of the sql statement (optimization)
  • Mysql updates certain fields of another table based on data from one table (sql statement)
  • MySQL intercepts the sql statement of the string function
  • Use SQL statements to query all database names, table names, and field names in MySQL, SQLServer, and Oracle
  • 10 SQL statement optimization techniques to improve MYSQL query efficiency
  • One sql statement completes MySQL deduplication and keeps one
  • MySQL statement arrangement and summary introduction

<<:  JS 9 Promise Interview Questions

>>:  Install Windows Server 2019 on VMware Workstation (Graphic Tutorial)

Blog    

Recommend

CentOS 7 Forgot Password Solution Process Diagram

need Whether it is a Windows system or a Linux sy...

How to use pdf.js to preview pdf files in Vue

When we preview PDF on the page, some files canno...

Semantic web pages XHTML semantic markup

Another important aspect of separating structure ...

Tips for implementing multiple borders in CSS

1. Multiple borders[1] Background: box-shadow, ou...

Detailed explanation of JavaScript's built-in Date object

Table of contents Date Object Creating a Date Obj...

HTML+CSS to achieve surround reflection loading effect

This article mainly introduces the implementation...

The difference between char, varchar and text field types in MySQL

In MySQL, fields of char, varchar, and text types...

Windows DNS server exposed "worm-level" vulnerability, has existed for 17 years

Vulnerability Introduction The SigRed vulnerabili...

Steps to build MHA architecture deployment in MySQL

Table of contents MAH 1. Introduction to MAH Arch...

React hooks pros and cons

Table of contents Preface advantage: shortcoming:...

Solution to no Chinese input method in Ubuntu

There is no solution for Chinese input method und...

Docker-compose creates a bridge, adds a subnet, and deletes a network card

1. Create a docker network card [root@i ~]# brctl...