码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 数据库创建表,查询表,对表进行增删改查,以及分组使用进行查询,还有排序


    文章目录

        • -- 通过DDL语句创建表
        • -- 插入一条学生记录:学号1,姓名王五,性别男,年龄10,出生日期2010-05-05,班级名”JJA2001”,选修java,分数70
        • -- 插入一条学生记录:学号3,姓名张三,性别男,年龄30,选修php
        • -- 删除表中王五学生信息
        • -- 修改表中所有学生的班级为设置为”JJA2002”
        • -- 修改表中王五的班级为”JPH2001”
        • -- 列出学生表所有信息
        • -- 列出所有学生的姓名和年龄
        • -- 列出所有学生的学号和姓名,并且显示为studentNo和studentName
        • -- 列出王五学生的姓名和年龄
        • - 列出”JJA2002”或者”JPH2001”的班级学生信息
        • - 列出不属于”JJA2001”班的学生信息
        • - 列出年龄在20-23岁之间的学生信息
        • - 列出没有分配班级的学生数据
        • - 列出”JPH2001”班级的女生姓名
        • - 查询姓“王”的学生
        • - 查询名字中带“强”的学生
        • - 按学生年龄升序
        • - 查询选修java的学生,按考试分数升序
        • - 查询”JJA2001”班的选修JAVA课程的学生成绩,按照降序显示。成绩相同,则按照学生编号升序。
        • 聚合函数
        • - 查询学生总年龄
        • - 查询学生平均年龄
        • - 查询学生最高年龄
        • - 查询学生最低年龄
        • - 查询”JJA2001”班的学生总数
        • - 查询班级列表
        • - 查询”JJA2001”班学生的总分
        • - 查询”JJA2001”班学生的平均分
        • - 列出男女生的人数
        • - 列出每个课程选修的人数
        • - 列出每个班级的人数
        • - 列出班级人数超过3个的班级名和人数
        • - 查询每门选修课程的平均成绩,并按照分数降序。
        • - 查询选课人数超过3个的选课列表,并按照人数降序。
        • 查询学生表中年龄前5个学生
        • 分数从高到低显示前2个课程最高分大于90分的课程列表和对应分数
        • 选修php课程成绩第三名的学生信息(无相同分数的情况)
        • 查询选修Java课程最后一名的学生信息(无相同分数的情况)
        • 查询年龄最高的学生信息
        • 查询Java课程成绩大于该课程平均分的学生列表
        • 查询班级平均分大于总平均分的班级列表
        • 查询班级平均分大于总平均分的班级数量

    – 通过DDL语句创建表

    create table hw_studentinfo(
    stu_no int(8) not null primary key auto_increment,
    stu_name varchar(10) not null,
    gender char(1),
    age int,
    birthday date,
    class varchar(10),
    course varchar(10),
    score float
    )

    – 插入一条学生记录:学号1,姓名王五,性别男,年龄10,出生日期2010-05-05,班级名”JJA2001”,选修java,分数70

    insert into hw_studentinfo
    values(1,“王五”,“男”,10,“2010-05-05”,“JJA2001”,“Java”,70);
    – 插入一条学生记录:学号2,姓名李四,性别女,年龄25,出生日期1997-01-05,班级名”JPH2001”,选修php
    insert into hw_studentinfo
    values(2,“李四”,“女”,25,“1997-01-05”,“JPH2001”,“php”,null);

    – 插入一条学生记录:学号3,姓名张三,性别男,年龄30,选修php

    insert into hw_studentinfo
    values(3,“张三”,“男”,30,null,null,“php”,null);
    insert into hw_studentinfo(stu_no,stu_name,gender,age,course)
    values(4,“张三”,“男”,30,“php”);

    – 删除表中王五学生信息

    delete from hw_studentinfo where stu_name = ‘王五’;

    – 修改表中所有学生的班级为设置为”JJA2002”

    update hw_studentinfo set class = “JJA2002”;

    – 修改表中王五的班级为”JPH2001”

    update hw_studentinfo set class = “JPH2001” where stu_name = ‘张三’;

    – 列出学生表所有信息

    select * from hw_studentinfo;

    – 列出所有学生的姓名和年龄

    select stu_name,age from hw_studentinfo;

    – 列出所有学生的学号和姓名,并且显示为studentNo和studentName

    select stu_no as studentNo,stu_name as studentName from hw_studentinfo;

    – 列出王五学生的姓名和年龄

    select stu_name,age from hw_studentinfo where stu_name = ‘张三’;

    - 列出”JJA2002”或者”JPH2001”的班级学生信息

    select * from hw_studentinfo where class = ‘JJA2002’ or class = ‘JPH2001’;
    select * from hw_studentinfo where class in(‘JJA2002’,‘JPH2001’);

    - 列出不属于”JJA2001”班的学生信息

    select * from hw_studentinfo where class != ‘JJA2001’;

    - 列出年龄在20-23岁之间的学生信息

    select * from hw_studentinfo where age between 20 and 23;

    - 列出没有分配班级的学生数据

    select * from hw_studentinfo where class is null;

    - 列出”JPH2001”班级的女生姓名

    select stu_name from hw_studentinfo where class = ‘JPH2001’ and gender = ‘女’;

    - 查询姓“王”的学生

    select * from hw_studentinfo where stu_name like ‘王%’;

    - 查询名字中带“强”的学生

    select * from hw_studentinfo where stu_name like ‘%强%’;

    - 按学生年龄升序

    select * from hw_studentinfo order by age asc;

    - 查询选修java的学生,按考试分数升序

    select * from hw_studentinfo where course = ‘Java’ order by score asc;

    - 查询”JJA2001”班的选修JAVA课程的学生成绩,按照降序显示。成绩相同,则按照学生编号升序。

    select * from hw_studentinfo where class = ‘JJA2001’ and course =‘java’
    order by score desc,stu_no asc;

    聚合函数

    - 查询学生总年龄

    select SUM(age) from hw_studentinfo;

    - 查询学生平均年龄

    select AVG(age) from hw_studentinfo;

    - 查询学生最高年龄

    select MAX(age) from hw_studentinfo;

    - 查询学生最低年龄

    select MIN(age) from hw_studentinfo;

    - 查询”JJA2001”班的学生总数

    select class,count(*) from hw_studentinfo where class = ‘JJA2001’;

    - 查询班级列表

    select DISTINCT class from hw_studentinfo;
    select class from hw_studentinfo group by class;

    - 查询”JJA2001”班学生的总分

    select class,sum(score) from hw_studentinfo where class = ‘JJA2001’;

    - 查询”JJA2001”班学生的平均分

    select class,avg(score) from hw_studentinfo where class = ‘JJA2001’;

    - 列出男女生的人数

    select gender,count(*) from hw_studentinfo group by gender;

    - 列出每个课程选修的人数

    select course,count(*) from hw_studentinfo group by course;

    - 列出每个班级的人数

    select class,count(*) from hw_studentinfo group by class;

    - 列出班级人数超过3个的班级名和人数

    select class,count() from hw_studentinfo group by class having count()>3;

    - 查询每门选修课程的平均成绩,并按照分数降序。

    select course,avg(score) from hw_studentinfo group by course order by avg(score) desc;

    - 查询选课人数超过3个的选课列表,并按照人数降序。

    select course,count() from hw_studentinfo
    group by course
    having count(
    ) > 3
    order by count(*) desc;

    查询学生表中年龄前5个学生

    select * from hw_studentinfo order by age desc limit 5;

    按照Java分数从高到低显示前5个学生
    select * from hw_studentinfo where course = ‘java’
    order by score desc limit 5;

    分数从高到低显示前2个课程最高分大于90分的课程列表和对应分数

    每一个课程的最高分
    select course,max(score) from hw_studentinfo
    group by course
    having max(score)>=90
    order by max(score) desc
    limit 2;

    选修php课程成绩第三名的学生信息(无相同分数的情况)

    select * from hw_studentinfo where course = ‘php’ order by score desc limit 2,1;

    查询选修Java课程最后一名的学生信息(无相同分数的情况)

    select * from hw_studentinfo where course = ‘JAVA’ order by score asc limit 1;

    查询年龄最高的学生信息

    select * from hw_studentinfo where age = (select max(age) from hw_studentinfo);

    查询Java课程成绩大于该课程平均分的学生列表

    select * from hw_studentinfo
    where course = ‘java’
    and score > (select avg(score) from hw_studentinfo where course = ‘java’)

    查询班级平均分大于总平均分的班级列表

    select class,avg(score)
    from hw_studentinfo
    group by class
    having avg(score) > (select avg(score) from hw_studentinfo)

    查询班级平均分大于总平均分的班级数量

    select COUNT(*) from (
    select class,avg(score)
    from hw_studentinfo
    group by class
    having avg(score) > (select avg(score) from hw_studentinfo)
    ) as temp;

  • 相关阅读:
    第9章Linux实操篇-组管理和权限管理
    8、IOC 之容器扩展点
    用什么项目管理软件科学统筹“双11”
    【SpringMVC】详细了解SpringMVC中WEB-INF 目录资源,视图解析器和静态资源放行的使用。
    Sparse Merkle Tree
    如何在国庆抓住收益红利?这家酒店集团最佳实践值得一看
    Python-新建-Django项目-调试-显示mysql数据库表内容-HelloWorld
    免费分享一个springboot+vue校园宿舍管理系统,挺漂亮的
    MySQL深分页
    3.1 Go语言中的函数与方法
  • 原文地址:https://blog.csdn.net/m0_59416550/article/details/126682614
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号