要删除表“ bar”中所有包含字符串“ foo”的行,您需要使用LIKE运算符。
为了理解上述语法,让我们创建一个名称为“ bar”的示例表。创建表的查询如下。创建下表后,我们将始终使用INSERT命令插入字符串为“ foo”的记录-
mysql> create table bar -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Words longtext, -> PRIMARY KEY(Id) -> );
现在,您可以使用insert命令在表中插入一些记录。插入记录时还会添加字符串“ foo”。查询如下-
mysql> insert into bar(Words) values('Javafoo'); mysql> insert into bar(Words) values('fooMySQL'); mysql> insert into bar(Words) values('Introductiontofoo C and C++'); mysql> insert into bar(Words) values('Introduction to Node.js'); mysql> insert into bar(Words) values('Introduction to Hibernate framework');
这是使用select语句显示表中所有记录的查询。查询如下-
mysql> select *from bar;
以下是输出-
+----+-------------------------------------+ | Id | Words | +----+-------------------------------------+ | 1 | Javafoo | | 2 | fooMySQL | | 3 | Introductiontofoo C and C++ | | 4 | Introduction to Node.js | | 5 | Introduction to Hibernate framework | +----+-------------------------------------+ 5 rows in set (0.00 sec)
这是从表“ bar”中删除所有包含字符串“ foo”的行的查询-
mysql> delete from bar where Words like '%foo' -> or Words like '%foo%' -> or Words like 'foo%';
现在再次检查表记录。查询如下-
mysql> select *from bar;
以下是输出-
+----+-------------------------------------+ | Id | Words | +----+-------------------------------------+ | 4 | Introduction to Node.js | | 5 | Introduction to Hibernate framework | +----+-------------------------------------+ 2 rows in set (0.00 sec)
现在看一下上面的示例输出,所有包含字符串“ foo”的记录都已从表“ bar”中删除。