在 MySQL 中的 SELECT 语句中追加通配符吗?


对于追加,使用 concat() 的概念。语法如下 −

select *from yourTableName where yourColumnName like concat('%',yourValue,'%');

让我们创建一个表 −

mysql> create table demo48
-> (
−> id int not null auto_increment primary key,
−> name varchar(20)
−> );
Query OK, 0 rows affected (0.70 sec)

使用 insert 命令将一些记录插入到表中 −

mysql> insert into demo48(name) values('John Smith');
Query OK, 1 row affected (0.12 sec)

mysql> insert into demo48(name) values('John Doe');
Query OK, 1 row affected (0.12 sec)

mysql> insert into demo48(name) values('Adam Smith');
Query OK, 1 row affected (0.23 sec)

mysql> insert into demo48(name) values('David Miller');
Query OK, 1 row affected (0.14 sec)

mysql> insert into demo48(name) values('Chris Brown');
Query OK, 1 row affected (0.11 sec)

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

mysql> select *from demo48;

这将生成以下输出 −

+----+--------------+
| id | name         |
+----+--------------+
|  1 | John Smith   |
|  2 | John Doe     |
|  3 | Adam Smith   |
|  4 | David Miller |
|  5 | Chris Brown  |
+----+--------------+
5 rows in set (0.00 sec)

以下是在 select 语句中追加通配符的查询 −

mysql> select *from demo48 where name like concat('%','Smith','%');

这将生成以下输出 −

+----+------------+
| id | name       |
+----+------------+
|  1 | John Smith |
|  3 | Adam Smith |
+----+------------+
2 rows in set (0.00 sec)

更新时间: 2020-11-19

99 人次浏览

开始你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.