Mysql中根据子表的分组后的最大统计数获取主表中的信息
2016-12-20 15:23
429 查看
连接mysql :mysql -h主机地址 -u用户名 -p密码
修改密码 :mysqladmin -u用户名 -p旧密码 password 新密码
...................还有简单的数据库操作不做例举
下面尝试一些数据库中相对比较复杂的查询语句。
案例1:我想获取某一个学生的全部信息,该学生的发帖数量最多。学生与帖子是俩个表,帖子关联学生的主键stu_id;
俩张表中的内容如下:
mysql> select pp_id,pp_title,stu_id from tb_postpage;
+-------+-----------+--------+
| pp_id | pp_title | stu_id |
+-------+-----------+--------+
| 1 | sdfasf | 1 |
| 2 | 一起啊 | 1 |
| 3 | sfsdg | 1 |
| 4 | jiayia | 2 |
| 6 | dasfasf | 2 |
| 7 | fasdfasdf | 3 |
| 8 | | 3 |
+-------+-----------+--------+
7 rows in set
mysql> select stu_id,stu_count,stu_password from tb_student;
+--------+-----------+--------------+
| stu_id | stu_count | stu_password |
+--------+-----------+--------------+
| 1 | 123456 | 123456 |
| 2 | 140950105 | 051315 |
| 3 | 13145 | pangjiafnei |
+--------+-----------+--------------+
1.先要对tb_postpage做一个分组。
select * from tb_postpage group by stu_id;
select pp_id,pp_title,stu_id from tb_postpage group by stu_id;
+-------+-----------+--------+
| pp_id | pp_title | stu_id |
+-------+-----------+--------+
| 1 | sdfasf | 1 |
| 4 | jiayia | 2 |
| 7 | fasdfasdf | 3 |
+-------+-----------+--------+
3 rows in set
2.获得每组分组之后的计数以及stu_id
mysql> select stu_id,count(*) as count from tb_postpage group by stu_id;
+--------+-------+
| stu_id | count |
+--------+-------+
| 1 | 3 |
| 2 | 2 |
| 3 | 2 |
+--------+-------+
3 rows in set
3.获取count中的最大值
mysql> select stu_id,max(count) from (select stu_id,count(*) as count from tb_postpage group by stu_id) aa;
+--------+------------+
| stu_id | max(count) |
+--------+------------+
| 1 | 3 |
+--------+------------+
1 row in set【注意,这里要为表起一个别名】
4.获取该表中的stu_id;
mysql> select stu_id from (select stu_id,max(count) from (select stu_id,count(*) as count from tb_postpage group by stu_id) aa) bb;
+--------+
| stu_id |
+--------+
| 1 |
+--------+
1 row in set
5.通过该id获取学生的全部信息
mysql> select stu_id,stu_count,stu_password from tb_student where stu_id = (select stu_id from (select stu_id,max(count) from (select stu_id,count(*) as count from tb_postpage group by stu_id) aa) bb);
+--------+-----------+--------------+
| stu_id | stu_count | stu_password |
+--------+-----------+--------------+
| 1 | 123456 | 123456 |
+--------+-----------+--------------+
1 row in set
修改密码 :mysqladmin -u用户名 -p旧密码 password 新密码
显示当前的数据库 :select database();
显示数据库版本 :select version();
获取当前时间: select now();...................还有简单的数据库操作不做例举
下面尝试一些数据库中相对比较复杂的查询语句。
案例1:我想获取某一个学生的全部信息,该学生的发帖数量最多。学生与帖子是俩个表,帖子关联学生的主键stu_id;
俩张表中的内容如下:
mysql> select pp_id,pp_title,stu_id from tb_postpage;
+-------+-----------+--------+
| pp_id | pp_title | stu_id |
+-------+-----------+--------+
| 1 | sdfasf | 1 |
| 2 | 一起啊 | 1 |
| 3 | sfsdg | 1 |
| 4 | jiayia | 2 |
| 6 | dasfasf | 2 |
| 7 | fasdfasdf | 3 |
| 8 | | 3 |
+-------+-----------+--------+
7 rows in set
mysql> select stu_id,stu_count,stu_password from tb_student;
+--------+-----------+--------------+
| stu_id | stu_count | stu_password |
+--------+-----------+--------------+
| 1 | 123456 | 123456 |
| 2 | 140950105 | 051315 |
| 3 | 13145 | pangjiafnei |
+--------+-----------+--------------+
1.先要对tb_postpage做一个分组。
select * from tb_postpage group by stu_id;
select pp_id,pp_title,stu_id from tb_postpage group by stu_id;
+-------+-----------+--------+
| pp_id | pp_title | stu_id |
+-------+-----------+--------+
| 1 | sdfasf | 1 |
| 4 | jiayia | 2 |
| 7 | fasdfasdf | 3 |
+-------+-----------+--------+
3 rows in set
2.获得每组分组之后的计数以及stu_id
mysql> select stu_id,count(*) as count from tb_postpage group by stu_id;
+--------+-------+
| stu_id | count |
+--------+-------+
| 1 | 3 |
| 2 | 2 |
| 3 | 2 |
+--------+-------+
3 rows in set
3.获取count中的最大值
mysql> select stu_id,max(count) from (select stu_id,count(*) as count from tb_postpage group by stu_id) aa;
+--------+------------+
| stu_id | max(count) |
+--------+------------+
| 1 | 3 |
+--------+------------+
1 row in set【注意,这里要为表起一个别名】
4.获取该表中的stu_id;
mysql> select stu_id from (select stu_id,max(count) from (select stu_id,count(*) as count from tb_postpage group by stu_id) aa) bb;
+--------+
| stu_id |
+--------+
| 1 |
+--------+
1 row in set
5.通过该id获取学生的全部信息
mysql> select stu_id,stu_count,stu_password from tb_student where stu_id = (select stu_id from (select stu_id,max(count) from (select stu_id,count(*) as count from tb_postpage group by stu_id) aa) bb);
+--------+-----------+--------------+
| stu_id | stu_count | stu_password |
+--------+-----------+--------------+
| 1 | 123456 | 123456 |
+--------+-----------+--------------+
1 row in set
相关文章推荐
- mysql分组后获取最大值的记录
- mysql group by分组,根据一个字段分组 ,又想得到另一个字段的最大或者最小。如何解决。
- mysql 分组取最大值的同时获取包含最大值的行的其他字段
- mysql分组查询获取组内某字段最大的记录
- mysql获取分组后每组的最大值
- Mysql分组后获取其中一个字段最大值的整条记录
- 获取数据表分组的最大值所在行所有信息的方法
- mysql根据某个字段分组根据更新时间获取最新的记录
- mysql sql 笔记-获取分组中某个字段的最大的行
- mysql获取分组后每组的最大值实例详解
- mysql根据身份证信息来获取用户属性信息
- mysql获取分组后每组的最大值
- 根据某字段分组,获取当前数据成功或失败各自的统计数
- 调用豆瓣的接口根据ISBN获取书籍信息
- mysql 获取当天发布的信息的语句
- 根据STATUS信息对MySQL进行优化
- Ajax特效 根据邮政编码获取地区信息 之四
- mysql获取数据库和表中信息及常用优化
- MySQL 获取 数据库 和 表信息 方法小结
- Ajax特效 根据邮政编码获取地区信息 之三