zoukankan      html  css  js  c++  java
  • mysql经常使用查询:group by,左连接,子查询,having where

    前几天去了两个比較牛的互联网公司面试。在sql这块都遇到问题了,哎。可惜呀,先把简单的梳理一下

    成绩表 score



    1、group by 使用

    按某一个维度进行分组

    比如:

    求每一个同学的总分

    SELECT student,SUM(score) FROM score GROUP BY student

    求每一个同学的平均分

    SELECT student,AVG(score) FROM score GROUP BY student

    也能够依照 班级,课程 来求


    2、having 与 where的差别

    having与where类似,能够筛选数据,where后的表达式怎么写,having后就怎么写
    • where针对表中的列发挥作用。查询数据
    • having对查询结果中的列发挥作用,筛选数据
    比如:

    查出挂了两门及以上的学生

    SELECT student,SUM(score<60)as gk FROM score GROUP BY student HAVING gk>1

    3、子查询

    (1)where子查询

    (把内层查询结果当作外层查询的比較条件)

    求比每门课程平均分低的学生

    SELECT student ,course, score 
    FROM score ,(SELECT course AS a_course,AVG( score)AS a_score FROM score GROUP BY course) AS avg_score
    WHERE course = a_course AND score<a_score


    先写到这吧

    能够參考

    http://www.cnblogs.com/rollenholt/archive/2012/05/15/2502551.html




  • 相关阅读:
    linq中的AsEnumerable()方法
    c# 一个匿名对象中包含多个子对象的处理方式
    jenkins的安装与启动
    牛客网-2018年湘潭大学程序设计竞赛-F
    poj-1149(最大流)
    hdu-2255(带权二分图)
    bzoj-1191(二分图最大匹配)
    codevs2822
    hdu 5652(并查集)
    hdu—3861(tarjan+二分图)
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6780915.html
Copyright © 2011-2022 走看看