使用 LIKE 在 MySQL 中选择行并创建包含匹配字符串的新列?
为此,使用 SUBSTRING()。我们首先创建一个表 −
mysql> create table DemoTable1872 ( Name varchar(20) ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令向表中插入一些记录 −
mysql> insert into DemoTable1872 values('John Doe');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1872 values('Adam Smith');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1872 values('Mitchell Johnson');
Query OK, 1 row affected (0.00 sec)使用 select 语句从表中显示所有记录 −
mysql> select * from DemoTable1872;
这将生成以下输出 −
+------------------+ | Name | +------------------+ | John Doe | | Adam Smith | | Mitchell Johnson | +------------------+ 3 rows in set (0.00 sec)
以下查询将选择带有 LIKE 的行并创建包含匹配字符串的新列 −
mysql> select Name,
substring(Name, locate('John', Name), length('John')) as NewName
from DemoTable1872
where Name like '%John%';这将生成以下输出 −
+------------------+---------+ | Name | NewName | +------------------+---------+ | John Doe | John | | Mitchell Johnson | John | +------------------+---------+ 2 rows in set (0.00 sec)
广告
数据结构
网络
关系型数据库
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP