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)

Recommend

Tutorial on building nextcloud personal network disk with Docker

Table of contents 1. Introduction 2. Deployment E...

vue-cropper component realizes image cutting and uploading

This article shares the specific code of the vue-...

Detailed explanation of JS homology strategy and CSRF

Table of contents Overview Same Origin Policy (SO...

Examples of correct use of interface and type methods in TypeScript

Table of contents Preface interface type Appendix...

Vue custom v-has instruction, steps for button permission judgment

Table of contents Application Scenario Simply put...

Detailed explanation of commonly used nginx rewrite rules

This article provides some commonly used rewrite ...

Summary of common docker commands

Docker installation 1. Requirements: Linux kernel...

Detailed explanation of MySQL group sorting to find the top N

MySQL group sorting to find the top N Table Struc...

Detailed explanation of the entry-level use of MySql stored procedure parameters

Use of stored procedure in parameters IN paramete...

How to display TIF format images in browser

The browser displays TIF format images Copy code T...

Detailed process of building mysql5.7.29 on centos7 of linux

1. Download MySQL 1.1 Download address https://do...

Implementation code for installing vsftpd in Ubuntu 18.04

Install vsftpd $ sudo apt-get install vsftpd -y S...