当前位置:   article > 正文

头歌MYSQL——课后作业3 数据查询(一)_头歌sqlserver第三章数据的查询

头歌sqlserver第三章数据的查询

第1关:按条件查询单表的所有字段

任务描述

本关任务:按条件查询数据表的所有字段 为了完成本关任务,你需要掌握:

如何查询数据表的所有字段

相关知识

查询数据表

命令格式: select * from 数据表 where 查询条件

任务要求

打开province数据库 第一题 查询街道信息(jdxx)数据表的开福区(qxmc)的所有字段 第二题 查询街道信息(jdxx)数据表的开福区和岳麓区(qxmc)的所有字段 第三题 查询街道信息(jdxx)数据表的长沙市(cs)的西湖街道(name)所有字段

jdxx数据表有四个字段,分别是省份(sf)、城市(cs)、区县(qxmc)、街道(name)。 例如,查询天心区(qxmc)的所有字段的值结果如图所示

jdxx数据


开始你的任务吧,祝你成功!

  1. use province;
  2. #代码开始
  3. #第一题
  4. select * from jdxx where qxmc='开福区';
  5. #第二题
  6. select * from jdxx where qxmc='开福区'||qxmc='岳麓区';
  7. #第三题
  8. select * from jdxx where cs='长沙市'&&name='西湖街道';
  9. #代码结束

 

 第2关:查询唯一值

  1. use province
  2. #代码开始
  3. select distinct qxmc from jdxx where sf='湖南省';
  4. select distinct qxmc from jdxx where cs='长沙市';
  5. #代码结束

第3关:统计查询

  1. use province;
  2. #代码开始
  3. #答案1
  4. select count(name) from jdxx where sf='湖南省';
  5. #答案2
  6. select count(name) from jdxx where cs='长沙市';
  7. #答案3
  8. select count(distinct qxmc) from jdxx where sf='湖南省';
  9. select count(distinct qxmc) from jdxx where cs='长沙市';
  10. #代码结束

 

 第4关:分组查询

 

  1. use province;
  2. select sf,count(*) from jdxx group by sf;
  3. #题目2
  4. select cs,count(*) from jdxx group by cs having count(*)>200;
  5. #题目3
  6. select qxmc,count(*) from jdxx where cs="长沙市" group by qxmc;

第5关:数据排序

 

  1. use province;
  2. #代码开始
  3. #第一题
  4. select * from jdtj order by jdgs desc limit 10;
  5. #第二题
  6. select * from jdtj order by jdgs asc limit 10;
  7. #第三题
  8. select * from jdtj where jdgs>35 order by jdgs desc,sf;
  9. #代码结束

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/490647
推荐阅读
相关标签
  

闽ICP备14008679号