mysql 除in以外的方法_如何删除除MySQL中的某些行以外的所有行?

您可以对不想删除的行使用NOT IN运算符。以下是语法-delete from yourTableName where yourColumnName NOT

IN(‘yourValue1’,‘yourValue2’,‘yourValue3’,.........N);

让我们首先创建一个表-mysql> create table deleteAllRowsWithCondition

-> (

-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,

-> Name varchar(20)

-> );

以下是使用insert命令在表中插入一些记录的查询-mysql> insert into deleteAllRowsWithCondition(Name) values('Larry');

mysql> insert into deleteAllRowsWithCondition(Name) values('John');

mysql> insert into deleteAllRowsWithCondition(Name) values('Sam');

mysql> insert into deleteAllRowsWithCondition(Name) values('Mike');

mysql> insert into deleteAllRowsWithCondition(Name) values('Carol');

mysql> insert into deleteAllRowsWithCondition(Name) values('Bob');

mysql> insert into deleteAllRowsWithCondition(Name) values('David');

以下是使用select语句显示表中所有记录的查询-mysql> select * from deleteAllRowsWithCondition;

这将产生以下输出-+----+-------+

| Id | Name  |

+----+-------+

| 1  | Larry |

| 2  | John  |

| 3  | Sam   |

| 4  | Mike  |

| 5  | Carol |

| 6  | Bob   |

| 7  | David |

+----+-------+

7 rows in set (0.00 sec)

这是删除具有某些条件的所有行的查询。我们不会在此处删除“ John”,“ Mike”和“ Carol”-mysql> delete from deleteAllRowsWithCondition where Name NOT IN('John','Mike','Carol');

让我们检查是否从表中删除了某些行。以下是查询-mysql> select * from deleteAllRowsWithCondition;

这将产生以下输出-+----+-------+

| Id | Name  |

+----+-------+

| 2  | John  |

| 4  | Mike  |

| 5  | Carol |

+----+-------+

3 rows in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值