数据库
一、分组查询
单表的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、表的关系
关系:讲的是表的关系!
关系型数据库:因为内部的表和表之间,数据&数据 可能在 一些关系!
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