码迷,mamicode.com
首页 > 数据库 > 详细

MySql单表查询

时间:2018-06-13 23:27:32      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:别名   value   group by   like   默认   有一个   mit   个数   姓名   

1.查询所有记录
select  *    from department;
select  *   from student;
select  *   from student_detail;

2.查询选中列记录

3.查询指定条件下的记录
select   s_name   from student where s_id>2;

4.查询后为列取别名
select  s_name as 姓名   from    student;

5.模糊查询

insert into student(s_name,dept_id ) values(‘张三丰‘,2),(‘小明‘,3),(‘小红‘,2);
 select * from student where s_name like ‘小%‘;

6.排序ORDER BY     :  ASC升序(默认)      DESC降序
降序:   select  *  from  `select`  order by `s_id` desc;

7.限制显示数据的数量LIMIT
#按学生学号升序输出的前4条数据
mysql> select * from `select` order by s_id limit 2;

#指定的返回的数据的位置和数量
mysql> select * from `select` order by s_id limit  2,2;


8.常用聚合函数
 #求最大年龄  
mysql> select  MAX(age)   from  student_detail;

#求最小年龄
mysql> select  MIN(age) from student_detail;

#求和
mysql> select  SUM(age)  from  student_detail;

#求平均数
mysql> select  AVG(age)   from  student_detail;

#四舍五入
mysql> select  ROUND(AVG(age))   from   student_details;

#统计
mysql> select count(s_id)   from  student;

9.分组查询GROUP BY

#对学生表中学院栏进行分组,并统计学院的学生人数:
mysql> select   dept_id  as 学院id,  count(dept_id)   as  学生个数 from   student  group by  dept_id;

HAVING分组条件
HAVING 后的字段必须是SELECT后出现过的

# 查看 哪些学院,只有一个学生
mysql> select  dept_id  as 学院id , count(dept_id)   as  学生个数 from   student    group  by   dept_id   having  学生个数=1;

 

MySql单表查询

标签:别名   value   group by   like   默认   有一个   mit   个数   姓名   

原文地址:https://www.cnblogs.com/mariobear/p/9180489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!