Summary: MySQL uses strings as a query condition

Source: Internet
Author: User
Tags mysql query

Preface : Today in the MySQL query encountered a problem, the situation is this, if now I have a table inside a field SCODE (indicating the product number), then there is a string stored multiple product number, such as "0001ME,0002BL" is a string, If it is called STR, it is intended to use in the way, that is, want to put Str as the condition, where scode in "0001ME,0002BL", but did not succeed, and then read the MySQL document, find various functions, and finally found the InStr function, Problem solved, let's talk about the result process

1. At the beginning, the string str has been converted to a similar in parameter, as follows

SELECT * from Stocks_orderlist s where S.scode in ("0001ME", "0002BL") LIMIT 10; Select CONVERT (CONCAT ("\ '", REPLACE ("0001ME,0002BL", "\,", "\ ' \,\ '"), "\"), CHAR); SELECT * from Stocks_orderlist s where S.scode in (CONVERT (CONCAT ("\" ", REPLACE (" 0001ME,0002BL "," \, "," \ "\,\" ")," \ ""), CHAR)) LIMIT 10;
Though I turned Str into what I wanted, I later thought that in the parameter is a list set, I promptly converted Str into the so-called in Parameter form, but certainly is a string, so busy for a long time, a little want to give up the feeling, but think back, must have not found the right way , so I found the InStr function.

2.instr function, look at the help document means to find out substr first problem in Str, try the following

Select INSTR ("0001ME,0002BL", "0003ME"); Select INSTR ("0001ME,0002BL", "0001ME"); Select INSTR ("0001ME,0002BL", "0002BL"); SELECT * from Stocks_orderlist s where INSTR ("0001ME,0002BL", S.scode);

summary : Looks very small one method, if does not know the method time is very painful ah.



Summary: MySQL uses strings as a query condition

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.