第四章数据库的查询.docVIP

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
  4. 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
  5. 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们
  6. 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
  7. 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
第四章数据库的查询

数据查询语言(DQL):其语句,也称为“数据检索语句”,用以从表中获得数据,确定数据怎样在应用程序给出。保留字SELECT是DQL(也是所有SQL)用得最多的动词,其他DQL常用的保留字有WHERE,ORDER BY,GROUP BY和HAⅥNG。这些DQL保留字常与其他类型的SQL语句一起使用。 select [distinct] 字段1,字段2,字段3,...字段n | * from 表 [where 查询条件的表达式 ] [grop by 分组字段 ] [having 分组后对每组增加数据的条件 ] [order by 排序字段|排序表达式 [asc|desc]] 4.2.1 选择列 1、选择指定的列 例: use studentdb; select stuname,profession,score from student; 2、定义列别名 例 定义列别名(在Mysql中定义别名只有这一种格式) select id as number,stuname as name,score as mark from student; 当列别名有空格时,别名的两端用单引号括起来。 select id as Student number,stuname as Student name,score as mark from student; 若列别名指定为大写时,不需要加单引号,大写也生效,如下例。 select id as NUMBER,stuname as NAME,score as MARK from student; 3、替换查询结果中的数据 case表达式的格式: case when 条件1 then 表达式1 when 条件2 then 表达式2 ... when 条件n then 表达式n else 表达式 end sql中case的用法 例:使用case语句替换查询出的原始数据。 select id,stuname,case when score is null then 尚未选课 when score 100 then 不及格 when score=100 and score=105 then 合格 else 优秀 end as 等级 from student where profession=计算机; 4、计算列值 创建课程表course create table course ( cno char (10) NOT NULL , cname varchar (30) , category varchar (30) , term char (2) , period int , credit double , PRIMARY KEY (cno)); 添加记录 insert into course values (12001,c++,基础课程,1,108,6) (12002,Java,专业核心课程,2,216,12); 创建学生课程表 create table studentcourse (id int NOT NULL, cno char (10) NOT NULL , result double , PRIMARY KEY (id, cno)); 添加记录: insert into studentcourse values(1,’12001’,98); 例 select id,cno,result*1.20 as 成绩 120 from studentcourse where id=’1’; 5、消除结果集中的重复行 例4.9 select distinct profession,score from student; 6、聚合函数(统计汇总的功能) 例4.10 select count(*) as 学生总数 from student; 例4.11 select count(comment) as 学生总数 from student; 例4.12 select count(score) as 总学分100分以上的人数 from student where score100; 或 select count(*) as 总学分100分以上的人数 from student where score100; 例4.13 select max(result),min(result) from studentcourse where cno=12001; 例4.14 select sum(result) as

文档评论(0)

zhanghc + 关注
实名认证
文档贡献者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档