《MySQL常用命令1 / 29 MySQL 常用命令汇总 http://www.database8.com 2011-3-1 2 / 29 Mysql 常用命令 show databases; 显示数据库 create database name; 创建数据库 use databasename; 选择数据库 drop database name 直接删除数据库,不提醒 show tables; 显示表 describe tablename; 显示具体的表结构 select 中加上 disti
在网上查找删除重复数据保留id最小的数据,方法如下:
DELETE
FROM
people
WHERE
peopleName IN (
SELECT
peopleName
FROM
people
GROUP BY
peopleName
HAVING
count(peopleName) > 1
)
AND peopleId NOT IN (
SELECT
min(peopleId)
刚开始,根据我的想法,这个很简单嘛,上sql语句
delete from zqzrdp where tel in (select min(dpxx_id) from zqzrdp group by tel having count(tel)>1);
执行,报错!!~!~
异常意为:你不能指定目标表的更新在FROM子句。傻了,MySQL 这样写,不行,让人郁闷。
难倒只能分步操作,蛋疼
以下是网友写的,同样是坑爹的代码,我机器上运行不了。
1. 查询需要删除的记录,会保留一条记录。
MYSQL里有五百万数据,但大多是重复的,真实的就180万,于是想怎样把这些重复的数据搞出来,在网上找了一圈,好多是用NOT IN这样的代码,这样效率很低,自己琢磨组合了一下,找到一个高效的处理方式,用这个方式,五百万数据,十来分钟就全部去除重复了,请各位参考。
第一步:从500万数据表data_content_152里提取出不重复的字段SFZHM对应的ID字段到TMP3表
create table tmp3 as select min(id) as col1 from data_conten
表结构: mysql> desc demo; +——-+——————+——+—–+———+—————-+ | Field | Type | Null | Key | Default | Extra | +——-+——————+——+—–+———+—————-+ | id | int(11) unsigned | NO | PRI | NULL | auto_increment | | site | varchar(100) | NO | MUL | | | +——-+——————+——+—
比如,表:event(id int(10) auto_increment primary key, sid int(10)not null, detail text)
我想删除表event中sid重复的记录,请问有没有这样SQL语句?或是通过其它方法? 代码如下:delete from event as e where id != (select min(id) from event where sid=e.sid); or
大家好,我是只谈技术不剪发的 Tony 老师。由于一些历史原因或者误操作,可能会导致数据表中存在重复的记录;今天我们就来谈谈如何查找 MySQL 表中的重复数据以及如何删除这些重复的记录。
创建示例表
首先创建一个示例表 people 并生成一些数据:
drop table if exists people;
create table people (
id int auto_increment primary key,
name varchar(50) not null,
email
表relation
create table relation(
id int primary key auto_increment,
userId int not null,
fanId int not null
);
插入几条数据
insert into relation(userId,fanId)
values(1,1) ,(1,1) ,(1,1), (2,2),(2,2) ,(3,3),(3,3);
表中的数据
id
userId
fanId
1
1
1
开发背景:
最近在做一个批量数据导入到MySQL数据库的功能,从批量导入就可以知道,这样的数据在插入数据库之前是不会进行重复判断的,因此只有在全部数据导入进去以后在执行一条语句进行删除,保证数据唯一性。
下面话不多说了,来一起看看详细的介绍吧
实战:
表结构如下图所示:
表明:brand
操作:
使用SQL语句查询重复的数据有哪些:
SELECT * from brand WHERE brandName IN(
select brandName from brand GROUP
例如: id name value 1 a pp 2 a pp 3 b iii 4 b pp 5 b pp 6 c pp 7 c pp 8 c iii id是主键 要求得到这样的结果 id name value 1 a pp 3 b iii 4 b pp 6 c pp 8 c iii 方法1 delete YourTable where [id] not in ( select max([id]) from YourTable group by (name + value)) 方法2 delet