MySQL:在示例表“bar”中删除包含字符串“foo”的所有行?
若要删除表“bar”中包含字符串“foo”的所有行,您需要使用 LIKE 运算符。
为了理解上述语法,让我们创建一个名为“bar”的示例表。创建表的查询如下。在创建下面的表之后,我们将始终使用 INSERT 命令插入包含字符串“foo”的记录−
mysql> create table bar -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Words longtext, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.61 sec)
现在,您可以使用 insert 命令在表中插入一些记录。在插入记录时也添加了字符串“foo”。查询如下−
mysql> insert into bar(Words) values('Javafoo');
Query OK, 1 row affected (0.14 sec)
mysql> insert into bar(Words) values('fooMySQL');
Query OK, 1 row affected (0.19 sec)
mysql> insert into bar(Words) values('Introductiontofoo C and C++');
Query OK, 1 row affected (0.22 sec)
mysql> insert into bar(Words) values('Introduction to Node.js');
Query OK, 1 row affected (0.19 sec)
mysql> insert into bar(Words) values('Introduction to Hibernate framework');
Query OK, 1 row affected (0.17 sec)以下是使用 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%'; Query OK, 3 rows affected (0.20 sec)
现在再次检查表记录。查询如下−
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”中删除。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP