如何删除包含下划线单词(如 MONTH_JAN')的记录?


为此,您可以使用 LIKE 运算符。让我们先创建一个表 -

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

使用 insert 命令向表中插入一些记录 -

mysql> insert into DemoTable(Name,BornMonth) values('Chris','MONTH_DEC');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(Name,BornMonth) values('Bob','MONTH_JAN');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(Name,BornMonth) values('Mike','MONTH_FEB');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable(Name,BornMonth) values('David','MONTH_JAN');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable(Name,BornMonth) values('Carol','MONTH_MARCH');
Query OK, 1 row affected (0.16 sec)

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

mysql> select *from DemoTable;

这将生成以下输出 -

+----+-------+-------------+
| Id | Name  | BornMonth   |
+----+-------+-------------+
|  1 | Chris | MONTH_DEC   |
|  2 | Bob   | MONTH_JAN   |
|  3 | Mike  | MONTH_FEB   |
|  4 | David | MONTH_JAN   |
|  5 | Carol | MONTH_MARCH |
+----+-------+-------------+
5 rows in set (0.00 sec)

以下是删除基于单词的记录的查询 -

mysql> delete from DemoTable where BornMonth LIKE '%MONTH_JAN%';
Query OK, 2 rows affected (0.18 sec)

让我们再次检查表记录 -

mysql> select *from DemoTable;

这将生成以下输出 -

+----+-------+-------------+
| Id | Name  | BornMonth   |
+----+-------+-------------+
|  1 | Chris | MONTH_DEC   |
|  3 | Mike  | MONTH_FEB   |
|  5 | Carol | MONTH_MARCH |
+----+-------+-------------+
3 rows in set (0.00 sec)

更新日期: 04-10-2019

130 次查看记录

开启你的职业生涯

通过完成课程获得认证

免费试用
广告