Home > Database > Mysql Tutorial > body text

MySQL删除重复数据行,只保存一条

WBOY
Release: 2016-06-07 16:24:21
Original
1490 people have browsed it

MySQL删除重复数据行,只保留一条 ? ? delimiter $$CREATE TABLE `devices_all` ( `device_all_id` int(11) NOT NULL AUTO_INCREMENT, `device_id` varchar(512) DEFAULT NULL, `device_token` varchar(512) DEFAULT NULL, `client_agent` varchar(512) DEFAU

MySQL删除重复数据行,只保留一条

?

?

delimiter $$

CREATE TABLE `devices_all` (
  `device_all_id` int(11) NOT NULL AUTO_INCREMENT,
  `device_id` varchar(512) DEFAULT NULL,
  `device_token` varchar(512) DEFAULT NULL,
  `client_agent` varchar(512) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  PRIMARY KEY (`device_all_id`)
) ENGINE=MyISAM AUTO_INCREMENT=49 DEFAULT CHARSET=latin1$$
Copy after login

?

表名devices_all,判重列device_id。?

?

delete from devices_all
where device_id in (select  device_id from  (select device_id from  devices_all  a  group by a.device_id having count(a.device_id) > 1) as a) 
and device_all_id not in (select device_all_id from (select min( device_all_id) device_all_id from devices_all group by  device_id having count( device_id)>1) as b)  
Copy after login

?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!