• 第十三周作业


    1、如何将 hellodb_innodb.sql导入到数据库中

     

    方法1:在命令行中

     

    [root@centos8 ~]#mysql < hellodb_innodb.sql 

     

    方法2:在数据库中

    MariaDB [(none)]> source hellodb_innodb.sql

    2、在学生表中,查询年龄大于25岁,且为男性的同学的名字和年龄

     

    MariaDB [hellodb]> SELECT name,age FROM students WHERE age > 25 AND gender='M';
    +--------------+-----+
    | name         | age |
    +--------------+-----+
    | Xie Yanke    |  53 |
    | Ding Dian    |  32 |
    | Yu Yutong    |  26 |
    | Shi Qing     |  46 |
    | Tian Boguang |  33 |
    | Xu Xian      |  27 |
    | Sun Dasheng  | 100 |
    +--------------+-----+
    7 rows in set (0.000 sec)

     

     

    3、在学生表中,以ClassID为分组依据,查询显示每组的平均年龄

     

    MariaDB [hellodb]> SELECT classid,avg(age) FROM students GROUP BY classid HAVING classid IS NOT NULL;
    +---------+----------+
    | classid | avg(age) |
    +---------+----------+
    |       1 |  20.5000 |
    |       2 |  36.0000 |
    |       3 |  20.2500 |
    |       4 |  24.7500 |
    |       5 |  46.0000 |
    |       6 |  20.7500 |
    |       7 |  19.6667 |
    +---------+----------+
    7 rows in set (0.000 sec)

     

     

    4、显示第3题中平均年龄大于30的分组及平均年龄

     

    MariaDB [hellodb]> SELECT classid,avg(age) FROM students GROUP BY classid HAVING classid IS NOT NULL AND avg(age) > 30;
    +---------+----------+
    | classid | avg(age) |
    +---------+----------+
    |       2 |  36.0000 |
    |       5 |  46.0000 |
    +---------+----------+
    2 rows in set (0.001 sec)

     

     

    5、显示以L开头的名字的同学的信息

     

    MariaDB [hellodb]> SELECT * FROM students WHERE name  LIKE 'L%';
    +-------+-------------+-----+--------+---------+-----------+
    | StuID | Name        | Age | Gender | ClassID | TeacherID |
    +-------+-------------+-----+--------+---------+-----------+
    |     8 | Lin Daiyu   |  17 | F      |       7 |      NULL |
    |    14 | Lu Wushuang |  17 | F      |       3 |      NULL |
    |    17 | Lin Chong   |  25 | M      |       4 |      NULL |
    +-------+-------------+-----+--------+---------+-----------+
    3 rows in set (0.000 sec)

     

     

    6、显示老师ID非空的同学的相关信息

     

    MariaDB [hellodb]> SELECT * FROM students WHERE teacherid IS NOT NULL;
    +-------+-------------+-----+--------+---------+-----------+
    | StuID | Name        | Age | Gender | ClassID | TeacherID |
    +-------+-------------+-----+--------+---------+-----------+
    |     1 | Shi Zhongyu |  22 | M      |       2 |         3 |
    |     2 | Shi Potian  |  22 | M      |       1 |         7 |
    |     3 | Xie Yanke   |  53 | M      |       2 |        16 |
    |     4 | Ding Dian   |  32 | M      |       4 |         4 |
    |     5 | Yu Yutong   |  26 | M      |       3 |         1 |
    +-------+-------------+-----+--------+---------+-----------+
    5 rows in set (0.000 sec)

     

     

    7、students表中,查询以年龄排序后的数据,并且显示年龄最大的前10位同学的信息

     

    MariaDB [hellodb]> SELECT * FROM students ORDER BY  age DESC LIMIT 10;
    +-------+--------------+-----+--------+---------+-----------+
    | StuID | Name         | Age | Gender | ClassID | TeacherID |
    +-------+--------------+-----+--------+---------+-----------+
    |    25 | Sun Dasheng  | 100 | M      |    NULL |      NULL |
    |     3 | Xie Yanke    |  53 | M      |       2 |        16 |
    |     6 | Shi Qing     |  46 | M      |       5 |      NULL |
    |    13 | Tian Boguang |  33 | M      |       2 |      NULL |
    |     4 | Ding Dian    |  32 | M      |       4 |         4 |
    |    24 | Xu Xian      |  27 | M      |    NULL |      NULL |
    |     5 | Yu Yutong    |  26 | M      |       3 |         1 |
    |    17 | Lin Chong    |  25 | M      |       4 |      NULL |
    |    23 | Ma Chao      |  23 | M      |       4 |      NULL |
    |    18 | Hua Rong     |  23 | M      |       7 |      NULL |
    +-------+--------------+-----+--------+---------+-----------+
    10 rows in set (0.000 sec)

     

    8、students表中,查询年龄大于等于20岁,小于等于25岁的同学的信息

     

    MariaDB [hellodb]> SELECT * FROM students WHERE age BETWEEN 20 AND 25;
    +-------+---------------+-----+--------+---------+-----------+
    | StuID | Name          | Age | Gender | ClassID | TeacherID |
    +-------+---------------+-----+--------+---------+-----------+
    |     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
    |     2 | Shi Potian    |  22 | M      |       1 |         7 |
    |     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
    |    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
    |    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
    |    17 | Lin Chong     |  25 | M      |       4 |      NULL |
    |    18 | Hua Rong      |  23 | M      |       7 |      NULL |
    |    21 | Huang Yueying |  22 | F      |       6 |      NULL |
    |    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
    |    23 | Ma Chao       |  23 | M      |       4 |      NULL |
    +-------+---------------+-----+--------+---------+-----------+
    10 rows in set (0.000 sec)

     

     

    9、以ClassID分组,显示每班的同学的人数

     

    MariaDB [hellodb]> SELECT classid,count(name) '每班人数' FROM students GROUP BY classid HAVING classid IS NOT NULL;
    +---------+--------------+
    | classid | 每班人数     |
    +---------+--------------+
    |       1 |            4 |
    |       2 |            3 |
    |       3 |            4 |
    |       4 |            4 |
    |       5 |            1 |
    |       6 |            4 |
    |       7 |            3 |
    +---------+--------------+
    7 rows in set (0.000 sec)

     

    10、以ClassID分组,显示其平均年龄大于25的班级

    MariaDB [hellodb]> SELECT classid,avg(age) '平均年龄' FROM students WHERE classid IS NOT NULL GROUP BY classid HAVING avg(age)>25;
    +---------+--------------+
    | classid | 平均年龄     |
    +---------+--------------+
    |       2 |      36.0000 |
    |       5 |      46.0000 |
    +---------+--------------+
    2 rows in set (0.000 sec)

     

     

  • 相关阅读:
    《.NET深入体验与实战精要》读书体会
    为什么周易中有64卦?
    16进制与8进制之间的快速转码
    3种夸克有多少组合?
    分辨率宽高和为整千?
    abt DVD
    为什么有20种氨基酸?
    HD与BD次时代之战!
    [转载]Java一些基础问题
    [转载]Java环境变量配置
  • 原文地址:https://www.cnblogs.com/jojohyj/p/13651945.html
Copyright © 2020-2023  润新知