您需要为此使用FIND_IN_SET()。让我们首先创建一个表-
mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerName varchar(20), CustomerAllProductPrice text );
使用insert命令在表中插入一些记录。在这里,我们要插入用逗号分隔的数字-
mysql> insert into DemoTable(CustomerName,CustomerAllProductPrice) values('Chris','245,345,678,90,45,56,78'); mysql> insert into DemoTable(CustomerName,CustomerAllProductPrice) values('Chris','98,99,90,56,77'); mysql> insert into DemoTable(CustomerName,CustomerAllProductPrice) values('David','1000,2000,4000,56000');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------------+--------------+-------------------------+ | CustomerId | CustomerName | CustomerAllProductPrice | +------------+--------------+-------------------------+ | 1 | Chris | 245,345,678,90,45,56,78 | | 2 | Chris | 98,99,90,56,77 | | 3 | David | 1000,2000,4000,56000 | +------------+--------------+-------------------------+ 3 rows in set (0.00 sec)
以下是查询以选择一行,该行的列中包含相同的数字,且数字集之间用逗号分隔-
mysql> select *from DemoTable where find_in_set('4000',CustomerAllProductPrice);
这将产生以下输出-
+------------+--------------+-------------------------+ | CustomerId | CustomerName | CustomerAllProductPrice | +------------+--------------+-------------------------+ | 3 | David | 1000,2000,4000,56000 | +------------+--------------+-------------------------+ 1 row in set (0.14 sec)