如果字符串匹配单元格内容则删除 MySQL 表格中列


首先,我们创建一个表 −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name varchar(100)
);
Query OK, 0 rows affected (0.55 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable(Name) values('John Smith');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(Name) values('Carol Taylor');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(Name) values('John Doe');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(Name) values('Chris Brown');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(Name) values('John Brown');
Query OK, 1 row affected (0.19 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

结果如下 −

+----+--------------+
| Id | Name         |
+----+--------------+
|  1 | John Smith   |
|  2 | Carol Taylor |
|  3 | John Doe     |
|  4 | Chris Brown  |
|  5 | John Brown   |
+----+--------------+
5 rows in set (0.00 sec)

以下是如果字符串匹配单元格内容则删除表中的列的查询 −

mysql> delete from DemoTable where Name like '%ohn%';
Query OK, 3 rows affected (0.16 sec)

我们再次检查表中的记录 −

mysql> select *from DemoTable;

结果如下 −

+----+--------------+
| Id | Name         |
+----+--------------+
|  2 | Carol Taylor |
|  4 | Chris Brown  |
+----+--------------+
2 rows in set (0.00 sec)

更新日期:2019 年 9 月 25 日

219 次浏览

开启你的 事业

完成课程即可获得认证

开始
广告
© . All rights reserved.