MySQL——SQL语句进阶

select * from 表
where 条件
group by 条件
order by 排序
limit

分组 Group by

select * from 表
group by 条件

结果为每个分组的第一条记录,该条记录作为该组的标志

select * from `subject` 
GROUP BY gradeid

MySQL——SQL语句进阶_第1张图片

select count(1),gradeid from `subject` 
GROUP BY gradeid

count(1) :在表后面新增加一列,全部赋值为1,再查询每组为1的个数
MySQL——SQL语句进阶_第2张图片

insert into subject (subjectname,classhour,gradeid) values(NULL,130,1);
select count(subjectname),gradeid from `subject` 
GROUP BY gradeid

count(subjectname) 查询subjectname不为空的格式,如果某条记录的subjename为空,查询个数时不计入
MySQL——SQL语句进阶_第3张图片

select count(distinct subjectname),gradeid from `subject` 
GROUP BY gradeid

distinct 去除分组中subjectname重复的记录

你可能感兴趣的:(MySQL,mysql,数据库)