如何使用 MySQL LIKE 子句获取以“Joh”开头的多个值
我们先创建一个表 −
mysql> create table DemoTable ( Name varchar(40) ); Query OK, 0 rows affected (0.55 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Ethan'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Johnson'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.12 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+---------+ | Name | +---------+ | John | | Sam | | Mike | | Ethan | | Johnson | | Bob | +---------+ 6 rows in set (0.00 sec)
以下是获取以“Joh”开头的多个值查询 −
mysql> select *from( select Name as myValue from DemoTable union select Name from DemoTable )tbl where myValue like 'Joh%';
这将产生以下输出 −
+---------+ | myValue | +---------+ | John | | Johnson | +---------+ 2 rows in set (0.00 sec)
广告