The difference between Find_in_set () and in MySQL

Source: Internet
Author: User

Get a test sheet to illustrate the difference.

CREATE TABLE ' test ' (  ' id ' int (8) NOT NULL auto_increment,  ' name ' varchar (255) is not NULL,  ' list ' varchar (255) N OT NULL,  PRIMARY KEY  (' id ')) INSERT INTO ' Test ' VALUES (1, ' name ', ' Daodao,xiaohu,xiaoqin '); insert INTO ' test ' VAL UES (2, ' name2 ', ' xiaohu,daodao,xiaoqin '); INSERT into ' Test ' VALUES (3, ' Name3 ', ' Xiaoqin,daodao,xiaohu ');

Originally thought that MySQL can make such a query:

Select ID, list, name from table where ' Daodao ' in (list); A

In fact, this is not possible, so that only if the name is the first element in the list, the query is valid, otherwise it will not get results, even if ' Daodao ' is really in the list.

Take a look at this again:

Select ID, list, name from table where ' Daodao ' in (' LIBK ', ' Zyfon ', ' Daodao '); Two

This is possible.

----------------------------------------------------------------

What is the difference between these two? Why the first one can't get the right result, and the second one can get the result.

The reason is (a) the (list) list is a variable , and (b) (' LIBK ', ' Zyfon ', ' Daodao ') is a constant .

So if you want to get (a) to work correctly, you need to use Find_in_set ():

Select ID, list, name from table where Find_in_set (' Daodao ', list); (i) an improved version.

Summarize:

So if the list is a constant, you can use in directly or use the Find_in_set () function.

The difference between Find_in_set () and in MySQL

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.