博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面试题: mysql 数据库去重 已看1 不好使
阅读量:6646 次
发布时间:2019-06-25

本文共 3688 字,大约阅读时间需要 12 分钟。

去重面试总结

前言:题目大概是这样的。

建表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE
TABLE
`test2` (
  
`id`
int
(11)
NOT
NULL
AUTO_INCREMENT,
  
`peopleId`
int
(11)
DEFAULT
NULL
,
  
`
name
`
varchar
(255)
DEFAULT
NULL
,
  
PRIMARY
KEY
(`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7
DEFAULT
CHARSET=utf8;
 
-- ----------------------------
-- Records of test2
-- ----------------------------
INSERT
INTO
`test2`
VALUES
(
'1'
,
'1'
,
'倒一'
);
INSERT
INTO
`test2`
VALUES
(
'2'
,
'1'
,
'倒一'
);
INSERT
INTO
`test2`
VALUES
(
'3'
,
'3'
,
'等等'
);
INSERT
INTO
`test2`
VALUES
(
'4'
,
'2'
,
'421'
);
INSERT
INTO
`test2`
VALUES
(
'5'
,
'2'
,
'421'
);
INSERT
INTO
`test2`
VALUES
(
'6'
,
'2'
,
'421'
);

1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断

网上答案:select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1)

mysql:select * from test2 where id in (select id from test2 group by peopleId having count(peopleId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(Id)来判断,只留有rowid最小的记录

网上答案:DELETE from 表 WHERE (id) IN ( SELECT id FROM 表 GROUP BY id HAVING COUNT(id) > 1) AND ROWID NOT IN (SELECT MIN(ROWID) FROM 表 GROUP BY id HAVING COUNT(*) > 1);

mysql:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
DELETE
FROM
test2
WHERE
peopleId
IN
   
select
a.peopleId
FROM
     
(
            
SELECT
*
FROM
test2
   
GROUP
BY
peopleId 
   
HAVING
count
(peopleId) > 1
        
)a
)
AND
id
NOT
IN
        
select
b.id
FROM
        
(
            
SELECT
*
FROM
test2
    
GROUP
BY
name 
    
HAVING
count
(
name
) > 1 
        
)b
 
)

3、查找表中多余的重复记录(多个字段)

select * from 表 a where (a.Id,a.seq) in(select Id,seq from 表 group by Id,seq having count(*) > 1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT
    
*
FROM
    
test3 a
WHERE
    
(a.id, a.seq)
IN
(
        
SELECT
            
id,
            
seq
        
FROM
            
test3
        
GROUP
BY
            
id,
            
seq
        
HAVING
            
count
(*) > 1
    
)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

delete from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count() > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count()>1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
DELETE
FROM
    
test3
WHERE
    
(id, seq)
IN
(
        
SELECT
            
a.id,
            
a.seq
        
FROM
            
(
                
SELECT
                    
id,
                    
seq
                
FROM
                    
test3
                
GROUP
BY
                    
id,
                    
seq
                
HAVING
                    
count
(*) > 1
            
) a
    
)
AND
(id, seq, `
name
`)
NOT
IN
(
    
SELECT
        
b.*
    
FROM
        
(
            
SELECT
                
*
            
FROM
                
test3
            
GROUP
BY
                
id,
                
seq
            
HAVING
                
count
(*) > 1
        
) b
)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

select * from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count() > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count()>1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
select
*
FROM
    
test3
WHERE
    
(id, seq)
IN
(
        
SELECT
            
a.id,
            
a.seq
        
FROM
            
(
                
SELECT
                    
id,
                    
seq
                
FROM
                    
test3
                
GROUP
BY
                    
id,
                    
seq
                
HAVING
                    
count
(*) > 1
            
) a
    
)
AND
(id, seq, `
name
`)
NOT
IN
(
    
SELECT
        
b.*
    
FROM
        
(
            
SELECT
                
*
            
FROM
                
test3
            
GROUP
BY
                
id,
                
seq
            
HAVING
                
count
(*) > 1
        
) b
)

胜负查询:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CREATE
TABLE
`t_game` (
  
`game_date`
varchar
(255)
DEFAULT
NULL
,
  
`game_res`
varchar
(255)
DEFAULT
NULL
) ENGINE=InnoDB
DEFAULT
CHARSET=utf8;
 
-- ----------------------------
-- Records of t_game
-- ----------------------------
INSERT
INTO
`t_game`
VALUES
(
'2018-03-20'
,
'胜'
);
INSERT
INTO
`t_game`
VALUES
(
'2018-03-20'
,
'胜'
);
INSERT
INTO
`t_game`
VALUES
(
'2018-03-20'
,
'负'
);
INSERT
INTO
`t_game`
VALUES
(
'2018-03-21'
,
'负'
);
INSERT
INTO
`t_game`
VALUES
(
'2018-03-21'
,
'胜'
);
INSERT
INTO
`t_game`
VALUES
(
'2018-03-21'
,
'负'
);
1
2
3
select
game_date,(
select
count
(*)
from
t_game
where
game_date = t.game_date
and
game_res =
'胜'
)
as
'胜'
,(
select
count
(*)
from
t_game
where
game_date = t.game_date
and
game_res =
'负'
)
as
'负'
 
from
t_game
as
t
group
by
game_date;

总结

1、delete不能有别名

2、mysql不支持又查又改,要用临时表

3、mysql不支持rowid

转载于:https://www.cnblogs.com/shan1393/p/9117702.html

你可能感兴趣的文章
北京点击科技有限公司董事长兼总裁——王志东经典语录5
查看>>
书籍推荐
查看>>
Linux误删home目录下的用户目录恢复
查看>>
敏捷安全10法
查看>>
saltstack 安装mysql
查看>>
学习数据仓库
查看>>
PHP ADOdb
查看>>
python list查询及所需时间
查看>>
通过PXE自动安装FreeBSD
查看>>
定制linux自动化安装镜像
查看>>
我的友情链接
查看>>
cacti监控NginxStatus并发状态汇总
查看>>
Samba服务器相关配置及实验过程
查看>>
STL源码剖析读书笔记之vector
查看>>
[2005.07.11 18:29:03] The experience I got in last week
查看>>
php直接读取数据库信息
查看>>
分支 判断素数
查看>>
DetachedCriteria的简单使用
查看>>
JavaScript中的函数是数据
查看>>
增加有规律的用户账号脚本
查看>>