• mysql高级查询


                          高级查询基本用法

    一、exists和notexists子查询

    1、exists子查询

    select 。。。。from 表名   where  exists(子查询);

    2、not exsits 子查询

    select 。。。。from 表名   where  not exists(子查询);

    二、子查询的注意事项

    1)子查询语句可以嵌套在sql语句中任何表达式出现的位置。

    在select语句中,子查询可以被嵌套在select语句的列,表,和查询条件中,即select子句、from子句、where子句、group by子句和having子句。

    ①子查询在select子句和from子句的使用语法。

    select (子查询)from 表名;

    子查询结果为单行单列,但不必指定列别名。

    ②子查询在from子句的使用语法。

    select * from (子查询) As 表的别名;

    注意:必须为表指定别名,一般返回多行多列数据记录,可以当做一张临时表。

    三、分组查询

    1、使用group by进行分组查询

    ①求各门课程学生的平均分:

    select subjectNo ,avg(subjectresult)

    from result

    group by subjectNo;

    ②按照性别进行分组:

    select count(*)AS人数,sex

    from student

    group by sex;

    ③查询每个年级的总人数

    select count(*) AS年级人数,gradeId

    from student

    group by gradeId;

    2、多列分组查询

    统计每个年级的男女学生人数:

    select grade AS年级,count(*) AS人数,sex AS性别

    from student 

    group by grade,sex

    order by grade;

    3、使用having子句进行分组筛选

    ①查询年级总人数超过2人的年级

    select count(*) AS人数,grade AS年级

    from student

    group by grade

    having count(*)>2;

     ②查询平均分达到及格的课程信息

    select subjectNo AS课程编号,avg(studentresult) AS平均分

    from result

    group by subject

    having avg(student_result)>=60;

    ③查询每门课程及格总人数和及格学生的平均分

    select count(*) AS总人数,avg(student_result)AS平均分,subjectNo AS课程

    from result

    where student_result>'60';

    group by subjectNo ;

    ④查询每门课程及格人数而且及格平均分在80分以上的记录。

    select count(*) AS 人数,avg(student_result)AS 平平均分,subjectNo AS课程

    from result

    where student_result>'60'

    group by subjectNo

    having avg(student_result)>80;

    四、多表连接查询

    1、多表连接的分类

    ①内连接查询

    使用 inner join或者where子句来 连接两个表

    (1)在where子句中指定连接条件

    如:查询学生姓名和成绩

    select student.name,result.student.result

    from student,result

    where student.studentNo=result.studentNo;

    (2)在from子句中使用inner  join 。。。on

    如:查询学生姓名和成绩

    select s.name,r.student_redult,r.studentNo

    from student AS s

    inner join result AS r on(s.studentNo=r.studentNo);

    2、外连接查询

    ①左外链接查询

    left join。。。on或者left outer join 。。。on

    如:以学生表为主表,成绩表为从表

    select s.name,r.student_redult,r.studentNo

    from student AS s

    left outer join result AS r

    on(s.studentNo=r.studentNo);

    2.由外连接查询

    right join。。on或者right outer join。。on

    如:以学生表为主表,成绩表为从表

    select s.name,r.student_redult,r.studentNo

    from student AS s

    right outer join result AS r

    on(s.studentNo=r.studentNo);

    五、外连接和内连接的区别

    http://www.cnblogs.com/Ewin/archive/2009/10/05/1578322.html

  • 相关阅读:
    arm activesync 串口(wince移植篇)
    请问生产成本收集器与标准成本评估有什么关联?
    BAPI / RFC with Delphi(系列之六)--TSAPFunctions使用BAPI创建PO(有登录对话框的delphi源代码)
    关于项目团队的发展阶段特点、问题、应对措施、领导风格
    如何在sap里设置打印机参数
    BAPI / RFC with Delphi(系列之四)--TSAPFunctions使用(有登录对话框的delphi源代码)
    BAPI / RFC with Delphi(系列之七)--TBAPIControl使用BUS1001显示物料(Delphi源代码)
    BAPI / RFC with Delphi(系列之五)--TSAPFunctions和TSAPLogoncontrol使用(无登录对话框Delphi源代码)
    Guice 1.0 用户指南
    BAPI / RFC with Delphi(系列之八)--TBAPIControl使用BUS2012建立PO(Delphi源代码)
  • 原文地址:https://www.cnblogs.com/story1/p/7655500.html
Copyright © 2020-2023  润新知