如何删除带下划线的单词(如MONTH_JAN')的记录?
为此,您可以使用LIKE运算符。让我们首先创建一个表-
create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(40), BornMonth varchar(40) );
使用插入命令在表中插入一些记录-
insert into DemoTable(Name,BornMonth) values('Chris','MONTH_DEC'); insert into DemoTable(Name,BornMonth) values('Bob','MONTH_JAN'); insert into DemoTable(Name,BornMonth) values('Mike','MONTH_FEB'); insert into DemoTable(Name,BornMonth) values('David','MONTH_JAN'); insert into DemoTable(Name,BornMonth) values('Carol','MONTH_MARCH');
使用select语句显示表中的所有记录-
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)
以下是删除基于单词的记录的查询-
delete from DemoTable where BornMonth LIKE '%MONTH_JAN%';
让我们再次检查表记录-
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)