MYSQL中利用select查詢某欄位中包含以逗號分隔的字串的記錄方法

來源:互聯網
上載者:User

標籤:

首先我們建立一張帶有逗號分隔的字串。 CREATE TABLE test(id int(6) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),pname VARCHAR(20) NOT NULL,pnum VARCHAR(50) NOT NULL);

然後插入帶有逗號分隔的測試資料
INSERT INTO test(pname,pnum) VALUES(‘產品1‘,‘1,2,4‘);
INSERT INTO test(pname,pnum) VALUES(‘產品2‘,‘2,4,7‘);
INSERT INTO test(pname,pnum) VALUES(‘產品3‘,‘3,4‘);
INSERT INTO test(pname,pnum) VALUES(‘產品4‘,‘1,7,8,9‘);
INSERT INTO test(pname,pnum) VALUES(‘產品5‘,‘33,4‘);


尋找pnum欄位中包含3或者9的記錄
mysql> SELECT * FROM test WHERE find_in_set(‘3‘,pnum) OR find_in_set(‘9‘,pnum);
+----+-------+---------+
| id | pname | pnum    |
+----+-------+---------+
|  3 | 產品3 | 3,4     |
|  4 | 產品4 | 1,7,8,9 |
+----+-------+---------+
2 rows in set (0.03 sec)


使用正則
mysql> SELECT * FROM test WHERE pnum REGEXP ‘(3|9)‘;
+----+-------+---------+
| id | pname | pnum    |
+----+-------+---------+
|  3 | 產品3 | 3,4     |
|  4 | 產品4 | 1,7,8,9 |
|  5 | 產品5 | 33,4    |
+----+-------+---------+
3 rows in set (0.02 sec)
這樣會產生多條記錄,比如33也被尋找出來了,不過MYSQL還可以使用正則,挺有意思的


find_in_set()函數返回的所在的位置,如果不存在就返回0
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)

還可以用來排序,如下;
mysql> SELECT * FROM TEST WHERE id in(4,2,3);
+----+-------+---------+
| id | pname | pnum    |
+----+-------+---------+
|  2 | 產品2 | 2,4,7   |
|  3 | 產品3 | 3,4     |
|  4 | 產品4 | 1,7,8,9 |
+----+-------+---------+
3 rows in set (0.03 sec)

如果想要按照ID為4,2,3這樣排序呢?
mysql> SELECT * FROM TEST WHERE id in(4,2,3) ORDER BY find_in_set(id,‘4,2,3‘);
+----+-------+---------+
| id | pname | pnum    |
+----+-------+---------+
|  4 | 產品4 | 1,7,8,9 |
|  2 | 產品2 | 2,4,7   |
|  3 | 產品3 | 3,4     |
+----+-------+---------+

3 rows in set (0.03 sec)          



DROP TABLE test_product;CREATE TABLE test_product(id int(6) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),pname VARCHAR(20) NOT NULL,pnum VARCHAR(50) NOT NULL);INSERT INTO test_product(pname,pnum) VALUES(‘product1‘,‘1,2,4‘);INSERT INTO test_product(pname,pnum) VALUES(‘product2‘,‘2,4,7‘);INSERT INTO test_product(pname,pnum) VALUES(‘product3‘,‘3,4‘);INSERT INTO test_product(pname,pnum) VALUES(‘product14‘,‘1,7,8,9‘);INSERT INTO test_product(pname,pnum) VALUES(‘product15‘,‘33,4‘);INSERT INTO test_product(pname,pnum) VALUES(‘產品2342‘,‘33,4‘);SELECT * FROM test_product WHERE find_in_set(‘33‘,pnum) OR find_in_set(‘9‘,pnum);



                    

MYSQL中利用select查詢某欄位中包含以逗號分隔的字串的記錄方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.