重生之我在地球Online当程序员16

数据库
一、分组查询

单表的CRUD,排序,去重,聚合函数

需求:统计男女分别有多少人!

男   4
女   2

语法:

select 分组的字段,聚合函数,group_concat(非分组字段) from 表名 [where条件] group by 分组的字段;

eg:

select gender,count(*),group_concat(student_name) from `students` group by gender;
select course_name,count(*),group_concat(student_name) from students group by course_name;

group_concat(student_name) 查看分组的细节!


二、限制查询

限制查询:limit

语法:

select * from 表名 [where条件] [order by 排序] [group by 分组字段] limit rows;select * from 表名 [where条件] [order by 排序] [group by 分组字段] limit index,rows;

eg:

#需求:查询一班中,考试成绩排名前3的学员
select * from stu_score where class_name = '一班' order by score desc limit 3;
#需求:查询一班中,第2页的剩余学生数据
select * from stu_score where class_name = '一班' order by score desc limit 5,5;

page 代表当前是第几页,rows 代表每页显示多少行!

limit index,rows;   //page = 1 index =0      rows = 5
                    //page = 2 index = 5     rows = 5
                    //page = 3 index = 10    rows = 5
                    //index = (page - 1) * rows;
三、表的关系
1、表的关系

image-20240529121027076

关系:讲的是表的关系!

关系型数据库:因为内部的表和表之间,数据&数据 可能在 一些关系!

1 对 多

多 对 多

1 对 1

1对1举例:

create table wife(
    id bigint primary key auto_increment,
    wife_name varchar(20)
);
create table husband(
    id bigint primary key auto_increment,
    hus_name varchar(20),
    fk_wife_id bigint
);

1对n举例:

create table cls(
    id bigint primary key auto_increment,
    cls_name varchar(20)
);
create table stus(
    id bigint primary key auto_increment,
    stu_na
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值