连接查询和分组查询

Posted on 2017-10-28 08:17  仙人板板  阅读(191)  评论(0编辑  收藏  举报

1.使用GROUP   BY进行分组查询

     语法

           select  studentid   avg(score)  from   student   group  by  studentid

           结果就会吧学生ID个样的分到一组,计算他们的平均成绩

2.多列分组查询

   例

       统计每个男生女生的人数,则理论上先把每个学期分开,在针对每个学期把男女生人数各自统计

         select   count(*)   sgrande     ssex     from   student   

         group   by   sgrande,ssex

         order  by    sgrade

3.使用having 子句进行分组查询

    (1)    select   count(*),sgrande    from   stufent   group by  sgrande

             having   count(*)>15

             查询年级总人数超过15人的年级

 

 多连接分类

       内连接查询

                   内连接查询是最典型的,最常用的链接查询

               语法

                  在where子句中指定连接条件

                   select   studentname ,score.courseid,score.score

                  where.  stident.scode =score.studentid

            在from子句中

                 select   student.name,score.studentid,score.score.  from.  student

 

                inner. join. score. on(student.scode=score.studentid)

     外连接查询

                  外连接查询是至少返回一个表中的所有记录,根据匹配条件有选择性的

                  返回另一张表的记录

          左外连接查询

              左外连接查询的结果集包括LEFT.  JOIN子句中指定的坐标的所有行,而不仅仅是连接列所匹配的行,若左表中没有匹配上,则在相关联的结果集行中右表的所有选择列均为空值

         语法

             SELECT. s.sname,c.couresid,c.score

             from.  student

             left.  outer.   join.   score.   on. student.scode. =c.studentid