String variables for MySQL query criteria

Source: Internet
Author: User

Original: http://blog.csdn.net/qing_gee/article/details/41646503

When your query condition is a string variable, what do you do, such as the string may be "0001ME,0002BL" or "0001ME", then how to get the desired result according to the condition, I will tell you the InStr method of the great.

1. At the beginning, the string str is converted to the parameters of the in function, 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);

String variables for MySQL query criteria

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.