美文网首页
写复杂sql的思路套路

写复杂sql的思路套路

作者: 刘程旭 | 来源:发表于2017-11-29 14:16 被阅读0次

    Group by

    Having

    Distinct

    Count

    Min

    Max

    Avg

    Sum

    where对from出来的表数据进行筛选

    group

    by是再where执行之后才执行

    having对group by的结果进行筛选

    SQL的执行顺序:

    [if !supportLists]1.[endif]From:视图1

    [if !supportLists]2.[endif]Where:视图2

    [if !supportLists]3.[endif]Group

    by:视图3

    [if !supportLists]4.[endif]Having:视图4

    [if !supportLists]5.[endif]Select:视图5

    [if !supportLists]6.[endif]Distinct:视图6

    [if !supportLists]7.[endif]Order

    by:视图7

    1.“增删改查”哪一种?

    select*

    2.从哪些表能够找全字段?

    from表1 a,表2 b

    3.是否是多表?

    wherea.字段1=b.字段2

    4.是否有字段约束?

    and字段名

    5.什么约束?

    =字段值

    6.是否有非全表的聚合函数?

    group by

    7.谁的聚合函数(按哪些字段进行聚合)?

    字段名1,字段名2

    8.将展现字段改为聚合字段

    select字段名1,字段名2

    9.对聚合函数是否有约束?

    having

    10.什么约束?

    聚合函数>条件

    12.最终要展现什么字段?

    字段+聚合函数

    13.是否去重?

    Distinct

    11.是否有排序?

    order by

    Student(S#, Sname,Sage,Ssex)学生表

    Course(C#,Cname,T#)课程表

    SC(S#,C#,score)成绩表

    Teacher(T#,Tname)教师表

    问题:

    [if !supportLists]1、[endif]查询“001”课程比“002”课程成绩高的所有学生的学号:

    [if !supportLists]2、[endif]查询平均成绩>60分的同学的学号和平均成绩

    SelectS#,avg(score)fromsc tgroup by S# having avg(score) >60

    查询“001”课程比“002”课程成绩高的所有学生的学号

    Selecta.S#from SC a , SC b where a.S#=b.S# and a.C#=’001’and b.C#=’002’

    [if !supportLists]1.[endif]“增删改查”哪一种?

    查询“001”课程比“002”课程成绩高的所有学生的学号

    select*

    [if !supportLists]2.[endif]从哪些表能够找全字段?

    查询“001”课程比“002”课程成绩高的所有学生的学号

    Select*fromSC

    a, SC b

    3.是否是多表?

    Select*from SC a, SC bwherea.sno=b.sno

    4.是否有字段约束?

    Select*from SC a, SC bwherea.sno=b.snoand a.C#=’001’ and b.C#=’002’

    5.什么约束?

    =字段值

    6.是否有非全表的聚合函数?

    group by

    7.谁的聚合函数(按哪些字段进行聚合)?

    字段名1,字段名2

    8.将展现字段改为聚合字段

    select字段名1,字段名2

    9.对聚合函数是否有约束?

    having

    10.什么约束?

    聚合函数>条件

    11.是否有排序?

    order by

    12.按什么字段排序?

    字段名

    13.最终要展现什么字段?

    字段+聚合函数

    Selecta.S#from SC a, SC bwherea.sno=b.sno and a.C#=’001’ and b.C#=’002’

    14.是否去重?

    distinct

    相关文章

      网友评论

          本文标题:写复杂sql的思路套路

          本文链接:https://www.haomeiwen.com/subject/ufjzvxtx.html