使用 MySQL 在 SELECT 中附加通配符?
mysqlmysqli database
对于附加,使用 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)
以下是在选择语句中附加通配符的查询 −
mysql> select *from demo48 where name like concat('%','Smith','%');
这将产生以下输出 −
+----+------------+ | id | name | +----+------------+ | 1 | John Smith | | 3 | Adam Smith | +----+------------+ 2 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

