您好,欢迎访问一九零五行业门户网

Mysql删除多表及多表记录sql语句

本文章总结了同时删除多个数据表与同时删除多个数据表的关系数据的方法,有需要的朋友可参考一下。
批量删除多表
删除所有pre_前缀的表
 代码如下 复制代码
select   concat( 'drop table ',table_name,'; ')   from   information_schema.tables where
information_schema.tables.table_name like 'pre_%' ;
删除所有pre_前缀的表 并且 不删除pre_uc前缀的表
 代码如下 复制代码
select   concat( 'drop table ',table_name,'; ')   from   information_schema.tables where
information_schema.tables.table_name like 'pre_%' and information_schema.tables.table_name not like
'pre_uc%';将得到的结果复制下来,再重新执行
删除多表同的数据
mysql数据库中,如果需要多张表同时删除数据,应该怎么做呢?下面就将为您介绍mysql中多表删除的方法,希望对您有所启迪。
1、从数据表t1中把那些id值在数据表t2里有匹配的记录全删除掉
 代码如下 复制代码
delete t1 from t1,t2 where t1.id=t2.id 或delete from t1 using t1,t2 where t1.id=t2.id
2、从数据表t1里在数据表t2里没有匹配的记录查找出来并删除掉
 代码如下 复制代码
delete t1 from t1 left join t2 on t1.id=t2.id where t2.id is null 或
delete from t1,using t1 left join t2 on t1.id=t2.id where t2.id is null
3、从两个表中找出相同记录的数据并把两个表中的数据都删除掉
 代码如下 复制代码
delete t1,t2 from t1 left join t2 on t1.id=t2.id where t1.id=25
注意此处的delete t1,t2 from 中的t1,t2不能是别名
如:
 代码如下 复制代码
delete t1,t2 from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25
在数据里面执行是错误的(mysql 版本不小于5.0在5.0中是可以的)
上述语句改写成
 代码如下 复制代码
delete table_name,table2_name from table_name as t1 left join table2_name as t2 on t1.id=t2.id where table_name.id=25
在数据里面执行是错误的(mysql 版本小于5.0在5.0中是可以的)
删除表中多余的重复记录,只留有rowid最小的记录(单字段)
 代码如下 复制代码
delete from 表
where 字段1 in (select 字段1 from 表 group by 字段1 having count(字段1) > 1) and
   rowid not in (select min(rowid) from 表 group by 字段1 having count(字段1) > 1)
删除表中多余的重复记录,只留有rowid最小的记录(多个字段)
 代码如下 复制代码
delete from 表 a
where (a.字段1, a.字段2) in (select 字段1, 字段2 from 表 group by 字段1, 字段2 having count(*) > 1) and
   rowid not in (select min(rowid) from 表 group by 字段1, 字段2 having count(*) > 1)
5.删除多于的重复记录(单个字段,多个字段)
 代码如下 复制代码
delete from table where id not in ( select min(id) from table group by name)
或者
delete from table where id not in ( select min(id) from table group by 字段1,字段2)
6.删除多余的重复记录(单个字段,多个字段)
 代码如下 复制代码
delete from table where id in ( select max(id) from table group by name having count(*)>1)
其它类似信息

推荐信息