1、 导入hellodb.sql生成数据库
导入数据库:mysql < hellodb.sql
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
MariaDB [hellodb]> select name 姓名,gender 性别,age 年龄 from students where age>25 and gender='m';
显示结果图:
(2) 以ClassID为分组依据,显示每组的平均年龄
MariaDB [hellodb]> select classid 课程号,avg(age) 平均年龄 from students group by classid; #注:应该是班级号
显示结果图:
(3) 显示第2题中平均年龄大于30的分组及平均年龄
MariaDB [hellodb]> select ClassID 课程号,avg(age) as 平均年龄 from students group by ClassID having avg(age)>30;
(4) 显示以L开头的名字的同学的信息
MariaDB [hellodb]> select * from students where name like 'L%';
显示结果图:
2、数据库授权magedu用户,允许10.0.8.0/24网段可以连接mysql
MariaDB [hellodb]> create user magedu@'10.0.8.%' identified by '000000'; #创建用户并设置密码为000000
在10.0.8.7上登录:
mysql -umagedu -h10.0.8.8 -p000000
允许用户登录时,默认用户的权限很小。为用户(magedu)添加对某个数据库(hellodb)的所有权限。
MariaDB [hellodb]> grant all on hellodb.* to magedu@'10.0.8.%';
补充:
5. 显示TeacherID非空的同学的相关信息
MariaDB [hellodb]> select * from students where teacherid is not null;
6.以年龄排序后,显示年龄最大的前10位同学的信息
MariaDB [hellodb]> select * from students order by age desc limit 10; #desc 倒叙
7.查询年龄大于等于20岁,小于等于25岁的同学的信息
MariaDB [hellodb]> select * from students where age between 20 and 25;
MariaDB [hellodb]> select * from students where age>=20 and age<=25;
8.以ClassID分组,显示每班的同学的人数
MariaDB [hellodb]> select classid 班级号,count(*) 总人数 from students group by classid;
9.以Gender分组,显示其年龄之和
MariaDB [hellodb]> select gender,sum(age) from students group by gender;
10.以ClassID分组,显示其平均年龄大于25的班级
MariaDB [hellodb]> select classid,avg(age) from students group by classid having avg(age)>25;