您的当前位置:首页正文

mysql条件删除语句

2021-03-29 来源:保捱科技网
mysql条件删除语句

MySQL是一种常用的关系型数据库管理系统,它支持使用条件删除语句来删除满足特定条件的数据。下面列举了10个符合标题内容的MySQL条件删除语句。

1. 删除表中满足条件的数据

```sql

DELETE FROM 表名 WHERE 条件; ```

例子:删除学生表中年龄大于20岁的数据

```sql

DELETE FROM students WHERE age > 20; ```

2. 删除表中的所有数据

```sql

DELETE FROM 表名; ```

例子:删除学生表中的所有数据

```sql

DELETE FROM students; ```

3. 删除表中重复的数据,只保留一条

```sql

DELETE t1 FROM 表名 t1, 表名 t2 WHERE 条件; ```

例子:删除学生表中重复的学号,只保留一条

```sql

DELETE t1 FROM students t1, students t2 WHERE t1.id > t2.id AND t1.number = t2.number; ```

4. 删除表中满足多个条件的数据

```sql

DELETE FROM 表名 WHERE 条件1 AND 条件2; ```

例子:删除学生表中年龄大于20岁且性别为女的数据

```sql

DELETE FROM students WHERE age > 20 AND gender = '女';

```

5. 删除表中满足某个条件范围的数据

```sql

DELETE FROM 表名 WHERE 列名 BETWEEN 值1 AND 值2; ```

例子:删除学生表中年龄在18到20岁之间的数据

```sql

DELETE FROM students WHERE age BETWEEN 18 AND 20; ```

6. 删除表中满足条件的前N条数据

```sql

DELETE FROM 表名 WHERE 条件 LIMIT N; ```

例子:删除学生表中年龄大于20岁的前5条数据

```sql

DELETE FROM students WHERE age > 20 LIMIT 5; ```

7. 删除表中满足条件的数据,并按照指定列排序

```sql

DELETE FROM 表名 WHERE 条件 ORDER BY 列名 ASC/DESC; ```

例子:删除学生表中年龄大于20岁的数据,并按照学号降序排序

```sql

DELETE FROM students WHERE age > 20 ORDER BY number DESC; ```

8. 删除表中满足条件的数据,并返回被删除的行数

```sql

DELETE FROM 表名 WHERE 条件 RETURNING *; ```

例子:删除学生表中年龄大于20岁的数据,并返回被删除的行数

```sql

DELETE FROM students WHERE age > 20 RETURNING *; ```

9. 删除表中满足条件的数据,并将删除的数据插入到另一个表中

```sql

DELETE 表名1 FROM 表名1 INNER JOIN 表名2 ON 条件 DELETE 表名1.*; ```

例子:删除学生表中年龄大于20岁的数据,并将删除的数据插入到备份表中

```sql

DELETE students FROM students INNER JOIN backup ON students.id = backup.id WHERE students.age > 20 DELETE students.*; ```

10. 删除表中满足条件的数据,并将删除的数据插入到另一个表中的特定列

```sql

DELETE 表名1 FROM 表名1 INNER JOIN 表名2 ON 条件 DELETE 表名1.列名; ```

例子:删除学生表中年龄大于20岁的数据,并将删除的数据中的姓名插入到备份表的姓名列

```sql

DELETE students FROM students INNER JOIN backup ON students.id = backup.id WHERE students.age > 20 DELETE students.name; ```

以上是10个符合标题内容的MySQL条件删除语句,可以根据具体的需求选择适用的语句来删除数据。

因篇幅问题不能全部显示,请点此查看更多更全内容