MySQL - 如果包含特定数字,如何选择所有记录?
如要选择所有带特定数字的记录,请在 MySQL 中使用 FIND_IN_SET()。
让我们创建一个表 -
示例
mysql> create table demo73 -> ( -> interest_id varchar(100), -> interest_name varchar(100) -> ); Query OK, 0 rows affected (1.48
在表中插入一些记录,借助于插入命令 -
示例
mysql> insert into demo73 values("100,101,103,105","SSC"); Query OK, 1 row affected (0.34 mysql> insert into demo73 values("105,103,1005,1003,104","Computer"); Query OK, 1 row affected (0.10 mysql> insert into demo73 values("110,105,104,111","Novel"); Query OK, 1 row affected (0.31
使用选择语句从表中显示记录 -
示例
mysql> select *from demo73;
这将产生以下输出 -
输出
+-----------------------+---------------+ | interest_id | interest_name | +-----------------------+---------------+ | 100,101,103,105 | SSC | | 105,103,1005,1003,104 | Computer | | 110,105,104,111 | Novel | +-----------------------+---------------+ 3 rows in set (0.00
以下查询可以选择如果包含特定数字的所有记录 -
示例
mysql> select *from demo73 where find_in_set("103",interest_id) > 0;
这将产生以下输出 -
输出
+-----------------------+---------------+ | interest_id | interest_name | +-----------------------+---------------+ | 100,101,103,105 | SSC | | 105,103,1005,1003,104 | Computer | +-----------------------+---------------+ 2 rows in set (0.00 sec)
广告