当前位置:Gxlcms > 数据库问题 > Mysql 练习 总结

Mysql 练习 总结

时间:2021-07-01 10:21:17 帮助过:19人阅读

 ()  比如  count();  where  寻找指定行: 

 

#---查询95033班和95031班全体学生的记录。

select * from  Student ,Course, Score where Student.Sno=Score.Sno and 

Course.Cno=Score.Cno and ( class=95033or class=95031)

关键词 :  

 逗号   很重要;   所有语句  几乎都可以嵌套使用

 select.. from   where   group by    having  ;   order by 

distanct    ; or    ;in  ;   and   ;  not 

 

1.not  :  not in (看前面 mysqle 基础)  和 not like

 #- 查询Student表中不姓“王”的同学记录。 select * from Student where Sname not like王% 

2. group by  和  where  , count(*)连用的时候 注意  [带小括号的表示方法}

技术分享
首先定义:聚集函数:一种函数,它对一组行中的某个列执行计算,并返回单个值。


1.where 和 having
简单的讲,where的条件是字段;而having 的条件可以是字段,也可以是聚集函数;
重要的是,where是筛选源数据,having多与group by 一起使用,并且条件常常是聚集函数;当有group by 时,having在group  by 条件的后面,而where 在group by的前面。
聚集函数:sum,count,avg ...等等;


2.count和sum
count 是‘累计’;  sum是‘累加’;
还是上面的表a_info中;
查询每个年级中分数大于60的有多少人以及他们的平均分,总分是多少:


    3.sql语句的执行顺序:
(1from    选取数据源;
(2where  筛选数据源;
(3)  group  by 将筛选的数据源分组;
(4)使用聚集函数计算;
(5)having 筛选分组的数据;
(6)计算表达式;
(7)order by 排序;
group where count(*)同时使用讲解
#----查询至少有2名男生的班号。

select class from Student  where Ssex= group by class having count(*)>=2

3.distanct: 使用方法 见  基础部分  不做赘述

4.获取时间 年月日 详见 下一篇  博客   以下简略 

获取的 为整数 可以进行大小比较:

获取年  year(时间所在列名)----列名不加引号

获取 当前时间  date(now())

获取 天        date(时间所在的列)

#----查询Student表中每个学生的姓名和年龄。
select Sname,year(now())-year(Sbrithday ) from Student

#---查询Student表中最大和最小的Sbirthday日期值。

select Sname, max(date(sbrithday)) from Student
union

select Sname, min(date(sbrithday)) from Student



#---以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from Student order by class,year(Sbrithday);
 

 

    

 

Mysql 练习 总结

标签:连接查询   lap   cte   回顾   display   整数   open   个学生   函数   

人气教程排行