设为首页 收藏本站
查看: 1021|回复: 0

[经验分享] mysql======基本的数据查询(1)

[复制链接]

尚未签到

发表于 2018-10-7 11:12:14 | 显示全部楼层 |阅读模式
  mysql 练习题
  ***创建student表
  mysql> create table student (

  ->>  -> name varchar(20) not null,
  -> sex varchar(4),
  -> brith year,
  -> deparment varchar(20),
  -> address varchar(50)
  -> );
  Query OK, 0 rows affected (0.03 sec)
  查看student表的结构
  mysql> desc student
  -> ;
  +-----------+-------------+------+-----+---------+-------+
  | Field     | Type        | Null | Key | Default | Extra |
  +-----------+-------------+------+-----+---------+-------+

  |>  | name     | varchar(20)  | NO   |     | NULL    |       |
  | sex      | varchar(4)  | YES  |     | NULL    |       |
  | brith     | year(4)    | YES  |     | NULL    |       |
  | deparment | varchar(20)  | YES    |     | NULL    |       |
  | address   | varchar(50)  | YES    |     | NULL    |       |
  +-----------+-------------+------+-----+---------+-------+
  6 rows in set (0.00 sec)
  ***创建score表
  mysql> create table score (

  ->>  -> stu_id varchar(20),
  -> c_name varchar(20),
  -> grade int(10)
  -> );
  Query OK, 0 rows affected (0.02 sec)
  查看score表的结构
  mysql> desc score;
  +--------+-------------+------+-----+---------+-------+
  | Field  | Type        | Null | Key | Default | Extra |
  +--------+-------------+------+-----+---------+-------+

  |>  | stu_id | varchar(20) | YES  |     | NULL    |       |
  | c_name | varchar(20) | YES  |     | NULL    |       |
  | grade  | int(10)     | YES  |    NULL        |
  +--------+-------------+------+-----+---------+-------+
  4 rows in set (0.00 sec)
  ****向student表中插入数据
  mysql> select * from student;
  +-----+--------+------+-------+-----------+--------------+

  |>  +-----+--------+------+-------+-----------+--------------+
  | 901 | 张老大 | 男   |  1985 | 计算机系  | 北京市海淀区 |
  | 902 | 张老二 | 男   |  1987 | 计算机系  | 北京市昌平区 |
  | 903 | 张三   | 女   |  1990 | 中文系    | 湖南省永州市 |
  | 904 | 李四   | 男   |  1990 | 英语系    | 辽宁省阜新市 |
  | 905 | 王五   | 女   |  1991 | 英语系    | 福建厦门市   |
  | 906 | 王六   | 男   |  1998 | 计算机系  | 湖南省衡阳市 |
  +-----+--------+------+-------+-----------+--------------+
  6 rows in set (0.03 sec)
  ****向score表中插入数据
  mysql> select * from score;
  +----+--------+--------+-------+

  |>  +----+--------+--------+-------+
  |  1 |    901 | 计算机 |    98 |
  |  2 |    901 | 英语   |    80 |
  |  3 |    902 | 计算机 |    65 |
  |  4 |    902 | 中文   |    88 |
  |  5 |    903 | 中文   |    95 |
  |  6 |    904 | 计算机 |    70 |
  |  7 |    904 | 英语   |    92 |
  |  8 |    905 | 英语   |    90 |
  +----+--------+--------+-------+
  8 rows in set (0.01 sec)
  ==============================================================
  ****题目1
  查询student表中所有记录
  mysql> select * from student;
  ****题目2
  查询student表的第二条到第四条记录
  mysql> select * from student limit 1,3;
  ****题目3
  从student表中查询所有学生的学号(id)姓名(name)、和院系(deparment)的信息.

  mysql> select>  ****题目4
  从student 表中查询计算机系和英语系的学生信息
  mysql> select * from student where deparment='计算机系' or  deparment='英语系';
  ****题目5
  从student表中查询出生年龄在26-30岁的学生信息

  mysql> select>  -> from student
  -> where 2017-brith between 26 and 30;
  ****题目6
  从student表中查询每个院系有多少人
  mysql> select deparment,count(id)
  -> from student
  -> group by deparment;
  ****题目7
  从score表中查询每个科目最高分
  mysql> select c_name, max(grade)
  -> from score
  -> group by c_name;
  ****题目8
  查询李四的考试(c_name)科目和考试成绩(grade)
  mysql> select c_name,grade
  -> from score
  -> where stu_id=(

  -> select>  -> where name='李四'
  -> );
  ****题目9
  用链接的形式查询所有学生的信息和考试成绩
  mysql> select student.id,name,sex,brith,deparment,address,c_name,grade
  -> from student, score
  -> where student.id=score.stu_id;
  ****题目10
  计算每个学生的总成绩降序排列
  mysql> select student.id,name,sex,brith,deparment,address,sum(grade)
  -> from student,score
  -> where student.id=score.stu_id
  -> group by name
  -> order by sum(grade) desc;
  ****题目11
  计算每个科目的平均成绩
  mysql> select student.id,name,sex,brith,deparment,address,avg(grade)
  -> from student,score
  -> where student.id=score.stu_id

  -> group by>  ****题目12
  查询计算机成绩低于95的学生信息
  mysql> select * from student

  -> where>  -> select stu_id from score
  -> where c_name='计算机' and grade select * from student

  -> where>  -> (select stu_id from score
  -> where stu_id in (
  -> select stu_id from
  -> score where c_name='计算机')
  -> and c_name='英语');
  ****题目14
  从student表和score表查询出学生的学号,然后合并查询结果

  mysql> select>  -> union
  -> select stu_id from score;
  ****题目15
  查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
  mysql> select student.id,name,sex,brith,deparment,address,c_name,grade
  -> from  student,score
  -> where (name like '张%' or name like '王%')
  -> and student.id=score.stu_id;
  ****题目16
  查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
  mysql> select student.id,name,sex,2017-brith as age,deparment,c_name,grade
  -> from student,score
  -> where address like '湖南%'
  -> and
  -> student.id=score.stu_id;


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-614266-1-1.html 上篇帖子: corosync+pacemaker+drbd+mysql实现高可用 下篇帖子: mysql=====mysql数据运算(2)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表