首页 > 代码库 > 6.5笔记-DQL高级查询

6.5笔记-DQL高级查询

一、高级查询
    Exists
    Drop table  if exists result;
    子查询有返回结果: EXISTS子查询结果为TRUE
    子查询无返回结果: EXISTS子查询结果为FALSE,
                       外层查询不执行
    1、分组查询
        group by  having
        #分组查询  查询每门课程的大于80的平均分 
        SELECT 
            sj.SubjectName AS 课程,
            avg(r.StudentResult) AS 平均分 
        FROM result AS r,`subject` AS sj
        WHERE r.SubjectNo = sj.SubjectNo
        GROUP BY r.SubjectNo 
        HAVING avg(r.StudentResult)>80
    having 可以单独使用(mysql可以,oracle数据库不行);
        SELECT * FROM student HAVING sex=1 AND GradeId=1
    2、内连接
        inner join  等值连接
        
    3、外连接
        左外连接 left join
            优先查询左边表的所有记录,不管和右边表是否有匹配记录
        右外连接 right join
        
        
        
        
        
        
        
        
        
        

 

6.5笔记-DQL高级查询