DQL query

Sort

Student inquiries, press the ages ascending, then descending in accordance with the results

select * from student order by age asc,score desc;

2 polymerizable functions

The total number of students query

select count(*) as 总人数 from student;

如果某一位置为nullYou can use IFNULL () function
queries the total number of students, a position is empty, it defaults to

select count(IFNULL(id,0)) from student;

3 packet

Grouped by gender, statistics Average scores for men and women

select gender ,AVG(score) from student group by gender;

当我们使用某个字段分组,查询也要将这个字段查出来,不然看不到数据属于哪组,分组有什么意义呢
Each demographic group, grouped according to gender

select gender,count(id) from student group by gender;

Queries older than 25 years old, grouped by gender, the number of statistics for each group, and display only the group sex is greater than 2

select gender,count(*) from student where age>25 group by gender having count(*) >2;
代码块
1.查询岗位名以及岗位包含的所有员工名字
select job_name ,GROUP_CONCAT(name) from employee GROUP BY job_name;

2.查询平均薪水大于10000的岗位及岗位平均薪资
select  job_name, AVG(salary) from employee GROUP BY job_name having avg(salary)>10000;

3.查询平均薪水大于10000的岗位和岗位平均薪资,并按照岗位薪资降序排列
select avg(salary) ,job_name  from employee GROUP BY job_name HAVING avg(salary)>10000 ORDER BY avg(salary) DESC;

4.查询雇员表所有信息,先按照年龄升序排列,再按照id降序排列
select * from employee ORDER BY age asc,id desc;

4 limit statement for pagination

Query student data, starting from the third, show 6

select * from student limit 2,6;

Guess you like

Origin www.cnblogs.com/hellosiyu/p/12501641.html