MySQL 如何使用MySQL LIKE子句检索以“Joh”开头的多个值

MySQL 如何使用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)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程