当前位置: 主页 > 数据库

修改数据库最大连接数-贵阳大数据数博会2018

发布时间:2023-03-09 22:10   浏览次数:次   作者:佚名

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

...................还有简单的数据库操作不做例举

下面尝试一些数据库中相对比较复杂的查询语句。

案例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