MySQL LIKE 查询带动态数组?


要实现带有动态数组的 LIKE 查询,语法如下 –

示例

select *from yourTableName
   where yourColumnName2 like "%yourValue%"
   order by yourColumnName1 asc
   limit yourLimitValue;

让我们创建一个表 –

示例

mysql> create table demo74
   -> (
   -> user_id int not null auto_increment primary key,
   -> user_names varchar(250)
   -> )
   -> ;
Query OK, 0 rows affected (0.67

借助 insert 命令向表中插入一些记录 –

示例

mysql> insert into demo74(user_names) values("John Smith1,John Smith2,John Smith3");
Query OK, 1 row affected (0.18

mysql> insert into demo74(user_names) values("John Smith1");
Query OK, 1 row affected (0.15

mysql> insert into demo74(user_names) values("David Smith1");
Query OK, 1 row affected (0.12

mysql> insert into demo74(user_names) values("John Smith1,John Smith2,John Smith3,John Smith4");
Query OK, 1 row affected (0.10

使用 select 语句从表中显示记录 –

示例

mysql> select *from demo74;

这将生成以下输出 –

输出

+---------+-------------------------------------------------+

| user_id | user_names                                      |

+---------+-------------------------------------------------+

|       1 | John Smith1,John Smith2,John Smith3             |

|       2 | John Smith1                                     |

|       3 | David Smith1                                    |

|       4 | John Smith1,John Smith2,John Smith3,John Smith4 |

+---------+-------------------------------------------------+

以下是带有动态数组的 MySQL LIKE 查询 –

示例

mysql> select *from demo74
-> where user_names like "%John Smith1%"
-> order by user_id asc
-> limit 100;

这将生成以下输出 –

输出

+---------+-------------------------------------------------+

| user_id | user_names                                      |

+---------+-------------------------------------------------+

|       1 | John Smith1,John Smith2,John Smith3             |

|       2 | John Smith1                                     |

|       4 | John Smith1,John Smith2,John Smith3,John Smith4 |

+---------+-------------------------------------------------+

3 rows in set (0.00 sec)

更新于:11-12-2020

1K+ 次观看

开启你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.