MYSQL uses select to query the notes that contain strings separated by commas (,) in a field.

Source: Internet
Author: User
In MYSQL, we use the select statement to query records that contain comma-separated strings in a field. First, we create a string with comma-separated strings. CREATETABLEtest (idint (6) NOTNULLAUTO_INCREMENT, PRIMARYKEY (id), pnameVARCHAR (20) NOTNULL, pnumVARCHAR (50) NOTNULL); then insert

In MYSQL, we use the select statement to query records that contain comma-separated strings in a field. First, we create a string with comma-separated strings. Create table test (id int (6) not null AUTO_INCREMENT, primary key (id), pname VARCHAR (20) not null, pnum VARCHAR (50) not null); then insert

How to Use select to query records that contain strings separated by commas in a field in MYSQL
First, create a string separated by commas.

Create table test (id int (6) not null AUTO_INCREMENT, primary key (id), pname VARCHAR (20) not null, pnum VARCHAR (50) not null );

Insert test data separated by commas (,).
Insert into test (pname, pnum) VALUES ('product 1', '1, 2, 4 ');
Insert into test (pname, pnum) VALUES ('product 2', '2, 4, 7 ');
Insert into test (pname, pnum) VALUES ('product 3', '3, 4 ');
Insert into test (pname, pnum) VALUES ('product 4', '1, 7,8, 9 ');
Insert into test (pname, pnum) VALUES ('product 5', '33, 4 ');


Search for records whose pnum field contains 3 or 9
Mysql> SELECT * FROM test WHERE find_in_set ('3', pnum) OR find_in_set ('9', pnum );
+ ---- + ------- + --------- +
| Id | pname | pnum ??? |
+ ---- + ------- + --------- +
|? 3 | product 3 | 3, 4 ???? |
|? 4 | product 4 | 1, 7, 8
+ ---- + ------- + --------- +
2 rows in set (0.03 sec)


Use Regular Expressions
Mysql> SELECT * FROM test WHERE pnum REGEXP '(3 | 9 )';
+ ---- + ------- + --------- +
| Id | pname | pnum ??? |
+ ---- + ------- + --------- +
|? 3 | product 3 | 3, 4 ???? |
|? 4 | product 4 | 1, 7, 8, 9 |
|? 5 | product 5 | 33,4 ??? |
+ ---- + ------- + --------- +
3 rows in set (0.02 sec)
This will generate multiple records, for example, 33 is also found, but MYSQL can also use regular expressions, which is quite interesting.


The position returned by the find_in_set () function. If it does not exist, 0 is returned.
Mysql> SELECT find_in_set ('E', 'H, e, l, l, O ');
+ ------------------------------ +
| Find_in_set ('E', 'H, e, l, l, O') |
+ ------------------------------ +
| ??????????????????????????? 2 |
+ ------------------------------ +
1 row in set (0.00 sec)

It can also be used for sorting, as shown below;
Mysql> SELECT * from test where id in (4, 2, 3 );
+ ---- + ------- + --------- +
| Id | pname | pnum ??? |
+ ---- + ------- + --------- +
|? 2 | product 2 | 2, 4, 7 ?? |
|? 3 | product 3 | 3, 4 ???? |
|? 4 | product 4 | 1, 7, 8, 9 |
+ ---- + ------- + --------- +
3 rows in set (0.03 sec)

What if you want to sort by ID 4, 2, 3?
Mysql> SELECT * from test where id in (, 3) order by find_in_set (id, '4 ');
+ ---- + ------- + --------- +
| Id | pname | pnum ??? |
+ ---- + ------- + --------- +
|? 4 | product 4 | 1, 7, 8, 9 |
|? 2 | product 2 | 2, 4, 7 ?? |
|? 3 | product 3 | 3, 4 ???? |
+ ---- + ------- + --------- +
3 rows in set (0.03 sec )?

Http://www.itokit.com/2012/0606/74328.html

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.