最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

面试笔试常考的mysql数据库操作groupby.

来源:懂视网 责编:小采 时间:2020-11-09 15:40:49
文档

面试笔试常考的mysql数据库操作groupby.

面试笔试常考的mysql数据库操作groupby.:IT 面试中,数据库的相关问题基本上属于必考问题,而其中关于sql语句也是经常考察的一个重要知识点。 下面介绍下sql语句中一个比较重要的操作group by,他的重要行一方面体现在他的理解困难度,一方面体现应用中的长见性。 首先,给出一个studnet学生
推荐度:
导读面试笔试常考的mysql数据库操作groupby.:IT 面试中,数据库的相关问题基本上属于必考问题,而其中关于sql语句也是经常考察的一个重要知识点。 下面介绍下sql语句中一个比较重要的操作group by,他的重要行一方面体现在他的理解困难度,一方面体现应用中的长见性。 首先,给出一个studnet学生

IT 面试中,数据库的相关问题基本上属于必考问题,而其中关于sql语句也是经常考察的一个重要知识点。 下面介绍下sql语句中一个比较重要的操作group by,他的重要行一方面体现在他的理解困难度,一方面体现应用中的长见性。 首先,给出一个studnet学生表: [s

IT 面试中,数据库的相关问题基本上属于必考问题,而其中关于sql语句也是经常考察的一个重要知识点。


下面介绍下sql语句中一个比较重要的操作group by,他的重要行一方面体现在他的理解困难度,一方面体现应用中的长见性。


首先,给出一个studnet学生表:

[sql] view plaincopyprint?

  1. CREATE TABLE `student` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(30) DEFAULT NULL,
  4. `sex` tinyint(1) DEFAULT '0',
  5. `score` int(10) NOT NULL,
  6. `dept` varchar(10) DEFAULT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

CREATE TABLE `student` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(30) DEFAULT NULL,
 `sex` tinyint(1) DEFAULT '0',
 `score` int(10) NOT NULL,
 `dept` varchar(10) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 


添加一些测试数据:


[sql] view plaincopyprint?

  1. mysql> select * from student where id<10;
  2. +----+------+------+-------+---------+
  3. | id | name | sex | score | dept |
  4. +----+------+------+-------+---------+
  5. | 1 | a | 1 | 90 | dev |
  6. | 2 | b | 1 | 90 | dev |
  7. | 3 | b | 0 | 88 | design |
  8. | 4 | c | 0 | 60 | sales |
  9. | 5 | c | 0 | 89 | sales |
  10. | 6 | d | 1 | 100 | product |
  11. +----+------+------+-------+---------+

mysql> select * from student where id<10;
+----+------+------+-------+---------+
| id | name | sex | score | dept |
+----+------+------+-------+---------+
| 1 | a | 1 | 90 | dev |
| 2 | b | 1 | 90 | dev |
| 3 | b | 0 | 88 | design |
| 4 | c | 0 | 60 | sales |
| 5 | c | 0 | 89 | sales |
| 6 | d | 1 | 100 | product |
+----+------+------+-------+---------+



给出需求,写出sql:

给出各个部门最高学生的分数。

要想得到各个部门学生,首先就要分组,按照部门把他们分组,然后在各个部门中找到分数最高的就可以了。


所以sql语句为:

[sql] view plaincopyprint?

  1. mysql> select *, max(score) as max from student group by dept order by name;
  2. +----+------+------+-------+---------+------+
  3. | id | name | sex | score | dept | max |
  4. +----+------+------+-------+---------+------+
  5. | 1 | a | 1 | 90 | dev | 90 |
  6. | 3 | b | 0 | 88 | design | 88 |
  7. | 4 | c | 0 | 60 | sales | 89 |
  8. | 6 | d | 1 | 100 | product | 100 |
  9. +----+------+------+-------+---------+------+
  10. 4 rows in set (0.00 sec)

mysql> select *, max(score) as max from student group by dept order by name;
+----+------+------+-------+---------+------+
| id | name | sex | score | dept | max |
+----+------+------+-------+---------+------+
| 1 | a | 1 | 90 | dev | 90 |
| 3 | b | 0 | 88 | design | 88 |
| 4 | c | 0 | 60 | sales | 89 |
| 6 | d | 1 | 100 | product | 100 |
+----+------+------+-------+---------+------+
4 rows in set (0.00 sec)


这只是个简单的例子,我们可以再把这个例子复杂化,比如分数最高的必须是女生,即sex列值必须为1才挑选出,这时的sql语句应该为:

[sql] view plaincopyprint?

  1. mysql> select *,max(score) as max from student group by dept having sex='1' order by name;
  2. +----+------+------+-------+---------+------+
  3. | id | name | sex | score | dept | max |
  4. +----+------+------+-------+---------+------+
  5. | 1 | a | 1 | 90 | dev | 90 |
  6. | 6 | d | 1 | 100 | product | 100 |
  7. +----+------+------+-------+---------+------+
  8. 2 rows in set (0.46 sec)

mysql> select *,max(score) as max from student group by dept having sex='1' order by name;
+----+------+------+-------+---------+------+
| id | name | sex | score | dept | max |
+----+------+------+-------+---------+------+
| 1 | a | 1 | 90 | dev | 90 |
| 6 | d | 1 | 100 | product | 100 |
+----+------+------+-------+---------+------+
2 rows in set (0.46 sec)


这里我们没有用where语句而是用了having,这里简单说明一下,因为我们的条件是在分组后进行的,其实分组前挑选出sex='1',然后再按照dept部门分组,也是可行的,这里就要看题目是怎么要求的:

[sql] view plaincopyprint?

  1. mysql> select *,max(score) as max from student where sex='1' group by dept order by name;
  2. +----+------+------+-------+---------+------+
  3. | id | name | sex | score | dept | max |
  4. +----+------+------+-------+---------+------+
  5. | 1 | a | 1 | 90 | dev | 90 |
  6. | 6 | d | 1 | 100 | product | 100 |
  7. +----+------+------+-------+---------+------+
  8. 2 rows in set (0.05 sec)

mysql> select *,max(score) as max from student where sex='1' group by dept order by name;
+----+------+------+-------+---------+------+
| id | name | sex | score | dept | max |
+----+------+------+-------+---------+------+
| 1 | a | 1 | 90 | dev | 90 |
| 6 | d | 1 | 100 | product | 100 |
+----+------+------+-------+---------+------+
2 rows in set (0.05 sec)


查询出的结果时一致的,如果把选择条件改为必须部门所有人的分数之和大于150才能把分数最高的部门的人列出来,这里就必须使用having了,因为 having 里面可以使用聚合函数sum,并且也必须分完组我们才能得到这个组的总分数,才能比较是否该值大于150:

[sql] view plaincopyprint?

  1. mysql> select *,max(score) as max from student group by dept having sum(score)>150 order by name;
  2. +----+------+------+-------+---------+------+
  3. | id | name | sex | score | dept | max |
  4. +----+------+------+-------+---------+------+
  5. | 1 | a | 1 | 90 | dev | 90 |
  6. | 6 | d | 1 | 100 | product | 100 |
  7. +----+------+------+-------+---------+------+
  8. 2 rows in set (0.00 sec)

mysql> select *,max(score) as max from student group by dept having sum(score)>150 order by name;
+----+------+------+-------+---------+------+
| id | name | sex | score | dept | max |
+----+------+------+-------+---------+------+
| 1 | a | 1 | 90 | dev | 90 |
| 6 | d | 1 | 100 | product | 100 |
+----+------+------+-------+---------+------+
2 rows in set (0.00 sec)



额外增加一个例子,比如我要选出不重复的部门,我们可以使用

[sql] view plaincopyprint?

  1. mysql> select distinct dept from student;
  2. +---------+
  3. | dept |
  4. +---------+
  5. | dev |
  6. | design |
  7. | sales |
  8. | product |
  9. +---------+
  10. 4 rows in set (0.02 sec)

mysql> select distinct dept from student;
+---------+
| dept |
+---------+
| dev |
| design |
| sales |
| product |
+---------+
4 rows in set (0.02 sec)


但是如果我们还要列出他的id等一些其他信息,我们如果这样:

[sql] view plaincopyprint?

  1. mysql> select name,distinct dept from student;
  2. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'distinct dept from student' at line 1

mysql> select name,distinct dept from student;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'distinct dept from student' at line 1

这是不行的,因为distinct只能放到开始位置,如果:

[sql] view plaincopyprint?

  1. mysql> select distinct dept,name from student;
  2. +---------+------+
  3. | dept | name |
  4. +---------+------+
  5. | dev | a |
  6. | dev | b |
  7. | design | b |
  8. | sales | c |
  9. | product | d |
  10. | product | m |
  11. +---------+------+
  12. 6 rows in set (0.00 sec)

mysql> select distinct dept,name from student;
+---------+------+
| dept | name |
+---------+------+
| dev | a |
| dev | b |
| design | b |
| sales | c |
| product | d |
| product | m |
+---------+------+
6 rows in set (0.00 sec)


为什么没有达到预期的效果,因为distinct 作用到了2个字段上,这时,我们就需要groub by 出场了。


[sql] view plaincopyprint?

  1. mysql> select dept,name from student group by dept;
  2. +---------+------+
  3. | dept | name |
  4. +---------+------+
  5. | design | b |
  6. | dev | a |
  7. | product | d |
  8. | sales | c |
  9. +---------+------+
  10. 4 rows in set (0.00 sec)

mysql> select dept,name from student group by dept;
+---------+------+
| dept | name |
+---------+------+
| design | b |
| dev | a |
| product | d |
| sales | c |
+---------+------+
4 rows in set (0.00 sec)


按照dept分组,自然就达到去重的目的了。所以有时候如果我们碰到了一个问题很难解决,比如用distinct去重,并带上其他列值,我们就需要尝试换个思路,可能答案自然就找到了。

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

面试笔试常考的mysql数据库操作groupby.

面试笔试常考的mysql数据库操作groupby.:IT 面试中,数据库的相关问题基本上属于必考问题,而其中关于sql语句也是经常考察的一个重要知识点。 下面介绍下sql语句中一个比较重要的操作group by,他的重要行一方面体现在他的理解困难度,一方面体现应用中的长见性。 首先,给出一个studnet学生
推荐度:
标签: 操作 面试 数据库
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top