美文网首页
Mysql常见50条数据查询

Mysql常见50条数据查询

作者: Spring_java | 来源:发表于2019-07-15 23:43 被阅读0次

1:-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

select student.*,a.score '课程01分数' ,b.score '课程02分数' from student 

LEFT JOIN score a on student.s_id=a.s_id and a.c_id='01'

LEFT JOIN score b on student.s_id=b.s_id and b.c_id='02' or b.c_id=null

WHERE a.score > b.score ;

-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT
    student.sname,
    student.s_id,
    ROUND(AVG(score.score), 1)
FROM
    student,
    score
WHERE
    student.s_id = score.s_id
GROUP BY
    student.s_id
HAVING
    AVG(score.score) >= 60;

-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

SELECT student.s_id,student.sname,COUNT(score.c_id),SUM(score.score) from student LEFT JOIN score ON

 student.s_id=score.s_id GROUP BY student.s_id,student.sname;
需要注意的是:此处使用left join  因为此时有的学生可能没有选课

-- 7、查询学过"张三"老师授课的同学的信息

SELECT
    student.*
FROM
    student LEFT join score ON student.s_id = score.s_id
AND score.c_id IN ( SELECT course.c_id FROM course WHERE course.t_id IN (
SELECT
                    teacher.t_id
                FROM
                    teacher
                WHERE
                    teacher.tname = '张三'
            )
    );

方法二:

SELECT student.* from student,score,course,teacher where student.s_id=score.s_id AND score.c_id=course.c_id and
course.t_id=teacher.t_id AND teacher.tname='张三';

-- 8、查询没学过"张三"老师授课的同学的信息

select * from student
   where s_id not in (
      select score.s_id from score where score.c_id  in (
      select course.c_id from course where course.t_id   = (
      select teacher.t_id from teacher where teacher.tname='张三' ))

);

-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

SELECT
    student.*
FROM
    student,
    score s1,
    score s2
WHERE
    student.s_id = s1.s_id
AND s1.c_id = '01'
AND student.s_id = s2.s_id
AND s2.c_id = '02'
AND s1.s_id = s2.s_id;

-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

SELECT student.* from student where student.s_id in 
(select score.s_id from score where score.c_id='01') 
AND student.s_id not in 
(select score.s_id from score where score.c_id ='02');

select student.* from student
join (select s_id from score where c_id =1 )tmp1
    on student.s_id=tmp1.s_id
left join (select s_id from score where c_id =2 )tmp2
    on student.s_id =tmp2.s_id
where tmp2.s_id is null;

-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

SELECT
    student.sname,
    student.s_id,
    ROUND(t1.avgScore, 1)
FROM
    student,
    (
        SELECT
            s_id,
            AVG(score) avgScore
        FROM
            score
        WHERE
            score < 60
        GROUP BY
            score.s_id
        HAVING
            COUNT(1) >= 2
    ) t1
WHERE
    t1.s_id = student.s_id;

-- 28、查询男生、女生人数

SELECT SUM(CASE WHEN ssex='男' then 1 else 0 end ) as 男生人数,SUM(CASE WHEN ssex='女' then 1 else 0 end ) as 女生人数 FROM student ;

select ssex,COUNT(ssex) from  student GROUP BY student.ssex;

-- 42、查询每门课程成绩最好的前两名

(select * from score where c_id ='01' order by s_score desc limit 2)
union (
select * from score where c_id ='02' order by s_score desc limit 2)
union (
select * from score where c_id ='03' order by s_score desc limit 2);

-- 35、查询所有学生的课程及分数情况

SELECT student.s_name , 
SUM(case when score.c_id ='01' then score.s_score else 0 END) '语文',
SUM(case when score.c_id ='02' then score.s_score else 0 END) '数学',
SUM(case when score.c_id ='03' then score.s_score else 0 END) '英语',
SUM(score.s_score) '总分'
FROM student,score,course where student.s_id=score.s_id AND score.c_id=course.c_id 
GROUP BY student.s_id

相关文章

  • SQL查询近期数据

    mysql查询时间的数据: 解释: 1.(mysql查询今天的数据) 2.(mysql查询昨天的数据) 3.(以当...

  • 从0到1学习网络安全 【MySQL基础-MySQL 查询数据】

    MySQL 查询数据 MySQL 数据库使用SQL SELECT语句来查询数据。你可以通过 mysql> 命令提示...

  • mysql查询优化

    查询优化在优化MySQL时,通常需要对数据库进行分析。常见的分析手段有慢查询日志,EXPLAIN分析查询,通过定位...

  • Python操作三大数据库(1)-MySQL

    本章是直接操作MySQL数据库的实现方式 连接MySQL数据库 查询单条数据 查询多条数据 通过分页方式查询 插入...

  • 2019-02-22

    mySQL Navicat for mySQL 关系型数据库:用表传数据 如何建表:查询→新建查询 注释: -- ...

  • python下MySQL的使用

    mysql连接 全部查询 单个查询 更新数据

  • mysql json 查询

    mysql json 查询 MySQL 5.7新增对JSON支持 1. 普通 json 查询 数据 查询语句 数...

  • 数据库的读锁和写锁在业务上的应用场景总结

    一、背景 熟悉MySQL数据库的朋友们都知道,查询数据常见模式有三种: 1. select ... :快照读,不加...

  • 9月17-MySQL性能优化

    MySQL性能优化策略 1、MySQL内核架构 2、索引原理与查询优化 加速MySQL高效查询数据的数据结构 二分...

  • SQL 基本知识

    SQL 是 结构化查询语言 常见的关系型数据库(RDBMS):Oracle、DB2、MySql、SQL Serve...

网友评论

      本文标题:Mysql常见50条数据查询

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