Solve the problem that one of MySQL string processing fields contains multiple IDs

Source: Internet
Author: User
Tags mysql delete

If a field in a MySQL table contains multiple IDs, how can this problem be solved? The following describes how to solve this MySQL string problem, I hope it will help you learn MySQL strings.

1. Create a table

 
 
  1. drop table if exists Category;  
  2. create table Category  
  3. (  
  4.     cateId                         int(5)                         not null AUTO_INCREMENT,  
  5.     chiName                        varchar(80),  
  6.    primary key (cateId)  
  7. );  
  8.  
  9. drop table if exists OpenRecord;  
  10. create table OpenRecord  
  11. (  
  12.     opreId                         int(5)                         not null AUTO_INCREMENT,  
  13.     cateIds                        varchar(80),  
  14.    primary key (opreId)                      
  15. );  

2. initialize data

 
 
  1. insert Category(chiName) values ('fish'),('shrimp'),('crab'),('tiger');  
  2.  
  3. insert OpenRecord(cateIds) values('1,2');  
  4. insert OpenRecord(cateIds) values('2,3');  

3. query the Category in OpenRecord where Id is 1.

# Incorrect method

 
 
  1. select *   
  2.     from Category  
  3.     where (select INSTR(cateIds,cateId) from OpenRecord where opreId=1) 

# Correct Method

 
 
  1. select *   
  2.     from Category  
  3.     where (select FIND_IN_SET(cateId,cateIds) from OpenRecord where opreId=1) 

When INSTR is used, when the ID is greater than 10, the data with ID 1 will be retrieved, and all the data with ID 1, 10, 11, 12 ...... will be taken out.

4. Problems with scaling.
Using FIND_IN_SET can solve the problem that IDs are separated. However, there are two other cases.

A. When IDs do not contain ",", but are separated by other symbols, for example, "| ". We have the following solutions:

 
 
  1. select *   
  2.     from Category  
  3.     where (select FIND_IN_SET(cateId,REPLACE(cateIds,'|',',')) from OpenRecord where opreId=1)  

The above is how to handle the MySQL string problem.

MySql stored procedure with Parameters

How to view the three MySQL character sets

Provides you with an in-depth understanding of the default MySQL character set.

How does MySQL Delete foreign key definitions?

Four conditions for using the MySQL foreign key

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.