我们能用一条 MySQL 查询获取到记录“Jone Deo”或“Deo Jone”吗?
为此,您可以使用 LIKE 子句。让我们首先创建一个表 -
mysql> create table DemoTable -> ( -> ClientName varchar(100) -> ); Query OK, 0 rows affected (0.85 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Smith John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Jone Deo'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Deo Jone'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
输出
这将产生以下输出 -
+-------------+ | ClientName | +-------------+ | John Smith | | Smith John | | Jone Deo | | Deo Jone | | Chris Brown | +-------------+ 5 rows in set (0.00 sec)
以下是如何使用一条 MySQL 查询获取记录“Jone Deo”或“Deo Jone” -
mysql> select *from DemoTable -> where ClientName -> LIKE -> (SELECT SUBSTRING_INDEX("%Jone% %Deo%", " ", -1)) -> or -> (SELECT SUBSTRING_INDEX("%Deo% %Jone%", " ", 1));
输出
这将产生以下输出 -
+------------+ | ClientName | +------------+ | Jone Deo | | Deo Jone | +------------+ 2 rows in set (0.00 sec)
广告