2014年11月21日 21:43:53

DQL

     基础查询--  注意要点:1.用户友善的标题
                                                                       例:   select  stuno as 学号  ,  studentname as 姓名 from student;
                                                           2.当我们要查询学生来自于哪些城市,出现重复数据  
                                                               查询非重复数据
                                                               我们使用关键字:distinct     
                                                                       例 : select distinct city from city ;

  1. select * from student; -- 所有列 大小写不区分-关键字、列名、表名
  2. select * from course;
  3. select stuNo, name, sex, address from student;
  4. --用户友善的列标题 列名 as "列标题"
  5. select stuNo as "学号", name as 姓名, sex 性别, address "地 址" from student;
  6. insert into student values('S9008', '张溜溜', null, null, null, null, null, 'M0001', null, '无锡');
  7. -- 查询学生来自于哪些城市
  8. -- 出现数据重复 查询非重复数据 distinct
  9. select distinct city from student;
  10. --select distinct city, address from student; -- 多列组成行数据不同


select语句经常和where语句联合使用
          -----where条件语句的运算符
                                    算术运算符
                                    并置运算符:       ||
                                    比较运算符
                                    逻辑运算符:     and  or   not
                                    其他运算符:     between...and..     not  in(100,200,300)
      注意要点:1.where javasescore >= 98 and javasescore <= 100   等价于   where javasescore between 98 and 100;
                       2.where city = '徐州' or city = '无锡'      等价于      where city in('徐州', '无锡');
                       3.并置运算符的写法: '学号: ' || stuNo   (先写友好名字)
                       4.SQL日期默认结构 10-10月-00
  1. -- 查询徐州学生
  2. select * from student where city = '徐州'
  3. -- 逻辑运算符 and or not
  4. select * from student where not city = '徐州' -- city != '徐州'
  5. select * from student where city = '徐州' or city = '无锡'
  6. -- 比较运算符
  7. -- 徐州 考试分数及格学生
  8. select * from student where city = '徐州' and javasescore >= 60 and sex = '女'
  9. -- 不及格学生
  10. select * from student where javasescore < 60
  11. select * from users where userName = 'admin' -- 判断用户名是否存在
  12. select password from users where userName = 'admin' -- 用户查找密码 - 输入密码比较
  13. --select * from users where userName = 'admin' and password = 'admin'
  14. -- 查询商品
  15. select * from product where kind = '电子' and skind = '手机' and brand = '手机'
  16. and size >= 4.7 and price >= 2000 and price <= 3000 and
  17. style = '4G'
  18. -- 其它运算符 between..and.. in(100,200,300)
  19. select * from student where javasescore >= 98 and javasescore <= 100
  20. select * from student where javasescore between 98 and 100; -- >= <=
  21. select * from student where city = '徐州' or city = '无锡'
  22. select * from student where city in('徐州', '无锡', '淮北', '南京') -- or
  23. -- 算术运算符
  24. -- S0001学生参与活动 考试成绩 + 20
  25. select stuNo, name, javasescore + 20 as score from student where stuNo = 'S0001';
  26. select pname, pprice * 0.5 from product;
  27. select empNo, empName, (salary * 0.2 + salary) as newSalary from employee;
  28. -- 并置运算符 || - String +
  29. select '学号: '||stuNo || ' 姓名: ' ||name || ' 城市: ' || city as stuInfo from student;
  30. -- SQL日期默认结构 10-10月-00 2000-10-10
  31. select * from student where birthdate = '10-10月-00'; -- to_date to_char




模糊查询
         关键字:  like    
     通配符:   %                     
         注意点: 1. %在哪边  哪边就有可能有字符,也可以没有
                               2. 当要查询的字符串中有   _ (下划线)   或者  %(百分号)
                                   需要使用:escape 声明出来    并且   需要使用 反斜杠 转义
                            例: select * from student where address like '%\_%' escape '\';
  1. select * from student where address like '%无锡%' -- 无锡% %无锡 %无%锡%
  2. select * from student where address like '无锡%'
  3. select * from student where address like '%无锡%18号%'
  4. select * from school where schoolname like '中国%大学'
  5. select * from school where schoolname like '中国__大学'
  6. -- 信息中包含 % _ 转义
  7. select * from student where address like '%\_%' escape '\'
  8. select * from student where address like '%\%%' escape '\'




检索遗漏值数据
            关键字: is  [not] null--- 是否为空
  1. -- 未参与考试学1生
  2. select * from student where javasescore is null;
  3. select * from student where cardNo is not null;



排序
   关键字:  order by   [要排序的列]  desc(降序)     asc(升序)
        注意要点: 1.如果order by [要排序的列]  之后不加升序还是降序关键字   默认是升序
                            2.order by 列名 [asc]|desc, 列名 [asc]|desc   -----   表示 : 先按照第一个列名排序 
                                                                                                                                如果相同 ,就按照第二个列名排序
  1. -- 参与考试学生成绩 从上之下排序
  2. select stuno, name, javasescore from student
  3. where javasescore is not null and javasescore >= 60
  4. order by javasescore, stuno desc; -- javasescore升序 相同按stuNo降序排序

多链表查询

  1. 等值连接:连接条件是等值唯一的  可以确定的(例:两个表之间的外键、主键之间一一对应相等......) ----通常使用 = 符号连接
  2. 非等值连接 : 连接条件是一个区间范围   ---- 通常使用 between ...and....  和 比较符号(< >=)来连接
  3. 外连接 : 当我们等值连接,某端数据多出后(例:要查学生和学生证的信息,但是如果有的学生没有学生证,则输出不了该学生的信息)  ---- 通常使用 在等值连接语句的一边加上    (+)  注意:不可以两边都加(+) 【ps:使用规律 :我们给等值语句的两边中少的一方添加该关键符号,比如上例中  没有学生证的学生是多出来的一方,就需要给学生证后面加上关键符号】    或者    使用  join on语句时,在join的前面加上   left   ||  right  ||  full【ps:使用规律 :当使用right或left时  我们要使其方向指向数据多出的一段,上例中应指向student】
  4. 自连接 : 表中列引用自身作为外键FK
  5. 自然连接:把列名相同作为公共条件     关键字  from [table1]  nature join  [table2]   (相同列的列名)
  6. 交叉连接 :  无条件 笛卡尔积  关键字  from [table1]  cross join  [table2]
代码:
  1. EQUIJOIN(等值联接) -- FK
  2. select s.stuno, s.name, m.majorname
  3. from student s, major m -- 笛卡尔积 行*行
  4. where s.majorNo = m.majorNo and s.stuno = 'S0001';
  5. -- join tableName on 公共条件
  6. select s.stuno, s.name, m.majorname
  7. from student s join major m
  8. on s.majorno = m.majorno
  9. --and s.stuno = 'S0001';
  10. where s.stuno = 'S0001';
  11. -- 中国大学 有哪些学生
  12. select s.schoolname, f.fname, m.majorname, st.stuno, st.name
  13. from school s, student st, faculty f, major m
  14. where s.schoolName = '中国大学' and
  15. s.schoolcode = f.schoolcode and
  16. f.facultyno = m.facultyno and
  17. m.majorno = st.majorno;
  18. select s.schoolname, f.fname, m.majorname, st.stuno, st.name
  19. from school s join faculty f on s.schoolcode = f.schoolcode
  20. join major m on f.facultyno = m.facultyno
  21. join student st on m.majorno = st.majorno
  22. where s.schoolname = '中国大学';
  23. -- 学生选择哪些课程
  24. NONEQUIJOIN(非等值联接) -- 范围
  25. select s.stuno, s.name, s.score, g.gradename, g.money
  26. from student s, grade g
  27. where s.score between g.minScore and g.maxScore
  28. and s.javasescore >= 60;
  29. --考试成绩评级
  30. OUTER JOIN (外联接) -- 等值联接 某端数据多出
  31. -- 所有学生及其学生证信息
  32. select *
  33. from student s, studentcard c
  34. --where s.cardno = c.cardno -- 值与值存在相同
  35. where s.cardno = c.cardno(+);
  36. select *
  37. from studentcard s right join student c -- left|right|full [outer] join tableName on ..
  38. on s.cardno = c.cardno;
  39. -- 查看所有学生及其学生会小组信息 学生未参与学生会小组
  40. select *
  41. from student s, studentunion n
  42. where s.unionno = n.unionno(+);
  43. -- 查看所有学会会小组信息 及其参与学生信息
  44. select *
  45. from student s, studentunion n
  46. where s.unionno(+) = n.unionno;
  47. -- 查询学校 所有学生会小组 所有学生的信息
  48. select *
  49. from student s, studentunion n
  50. where s.unionno = n.unionno(+)
  51. union
  52. select *
  53. from student s, studentunion n
  54. where s.unionno(+) = n.unionno;
  55. select *
  56. from student s full join studentunion n
  57. on s.unionno = n.unionno;
  58. -- BBS 帖子Post 回复Reply 查询所有帖子及其回复的信息
  59. SELF JOIN (自联接) -- 表中列引用自身列作为FK
  60. select s.stuNo, s.name, t.name
  61. from student s, student t
  62. where t.stuno = s.monitorNo;
  63. NATURAL JOIN (自然联接) -- 把列名相同作为公共条件
  64. select *
  65. from student s NATURAL join major m;
  66. select *
  67. from student s join major m using(majorNo); -- on s.majorNo = m.majorNo
  68. CROSS JOIN (交叉联接) -- 无条件 笛卡尔积
  69. select *
  70. from student CROSS join major;


子查询
  •     单行子查询      单行子查询  一般使用 = 号
  •   多行子查询      多行子查询  一般使用 in (1.  因为子查询结果可能有多个   若使用=号   会因为返回结果多,而报错,使用in  会返回所有结果       2.若比较范围是number类型的值  那么需要使用>any  <any   >all  <all)其中>any   是指大于子查询结果中的任意一个值,等价于最小值       >all指的是大于子查询结果中的所有值,等价于大于最大值。

【ANY】

“比任意一个销售员工资低”==“比最高销售员工资低”;

“比任意一个销售员工资高”==“比最低销售员工资高”;

【ALL】

“比所有销售员工资都低”==“比最低销售员工资低”;

“比所有销售员工资都高”==“比最高销售员工资高”;

  1. --单行子查询 条件 PK UK
  2. -- 学校名 中国三美大学 - 学校有哪些院系
  3. select s.schoolName, f.fname
  4. from school s, faculty f
  5. where s.schoolCode = f.schoolCode
  6. and s.schoolname = '中国三美大学';
  7. -- 给出条件不可直接作为条件操作 - 可通过给定条件查询出所需条件
  8. -- 比较运算符 子查询结果必须为单行
  9. select f.facultyNo, f.fname -- 院系编号、名称
  10. from faculty f
  11. where f.schoolcode = (select schoolCode from school
  12. where schoolName = '中国三美大学'); -- 子查询 (子查询) 右边
  13. -- 中国大学 新开院系 艺术系
  14. insert into faculty values('05', '艺术系',
  15. (select schoolCode from school where schoolname = '中国大学'));
  16. -- 往 体育新闻 增加新新闻稿
  17. -- 查看 体育新闻 中新闻稿
  18. -- 把市场部员工工资 + 500
  19. -- 中国大学 已新开院系 艺术系 加入美声音乐专业
  20. insert into major values('M8899', '美声音乐',
  21. (select facultyno from faculty where fname = '艺术系'
  22. and schoolcode =
  23. (select schoolcode from school
  24. where schoolname = '中国大学')));
  25. -- BBS 查看Java学习模块 某用户 发的帖子
  26. -- 多行子查询
  27. -- 不可使用 比较运算符
  28. -- 多行运算符
  29. in =any
  30. -- 报考大学 若有 艺术系 优先考虑
  31. select s.schoolcode, s.schoolname
  32. from school s
  33. where s.schoolcode in (select schoolCode from faculty
  34. where fname = '艺术系');
  35. >any <any -- 数字
  36. >all <all
  37. -- 查询学生中分数比张三三学生低的学生
  38. select s.stuNo, s.name, s.javasescore
  39. from student s
  40. where s.javasescore <all (select javasescore from student
  41. where name = '张三三');


Top - n查询
    -- rownum 伪列  默认为数据库中第一行至最后一行   *从1开始*
    -- rownum 必须从第一条件开始 
    -- rownum 可以隐式  不写

  1. -- rownum 必须从第一条件开始
  2. select rownum, stuNo, name, score from student where rownum <= 3; -- 获取数据库表中前三条数据
  3. -- 成绩前三名
  4. select *
  5. from (select stuNo, name, score from student order by score desc) -- 虚拟临时表 内联视图
  6. where rownum <= 3;


分页数据查询  
     -- rownum as name 
    -- 当前页面 只显示当前页面数据 - 查询当前页面数据  
                      
  1. -- 查询学生及其专业 数据 每个页面显示2条数据
  2. select stuNo, name, score, majorName
  3. from (select rownum as num, s.stuNo, s.name, s.score, m.name as majorName
  4. from student s, major m
  5. where s.majorno = m.majorno)
  6. where num >= 1 and num <= 5;
  7. -- mysql select ... limit 1, 5;
  8. -- 查询学生及其专业 数据 按学生成绩降序排序 每个页面显示2条数据
  9. select stuNo, name, score, majorName
  10. from (select rownum as num, stuNo, name, score, majorName
  11. from (select s.stuNo, s.name, s.score, m.name as majorName
  12. from student s, major m
  13. where s.majorno = m.majorno
  14. order by s.score desc))
  15. where num >= 4 and num <= 6;

group 分组查询 
                   获取组合中数据  -  整体信息
                        group by ...   +  多行函数 
注意点:
  1. 关键词  count(*)或者count(指定的属性)
  2. group by  后面列出所有的单行函数
  3. 关键词  avg()  算出平均值
  4. 在group by 语句完成后   可以跟having avg(.....)来取指定范围的平均值的数据
  1. -- 查看 学校中 每个专业信息 学生数
  2. select m.majorno, m.name, count(*), avg(s.javaseScore) as avgScore -- 单行、多行函数结合 必须使用group by
  3. from major m, student s
  4. where m.majorno = s.majorno
  5. group by m.name, m.majorno -- select中单行函数必须在group by中出现
  6. --order by m.majorno desc -- group by中单行函数可不在select中
  7. having avg(s.javaseScore) < 80 -- 对分完组数据 条件操作 having 平均分不及格
  8. order by avgScore
  9. --order by avg(s.javaseScore) desc
  10. -- 学生 项目组
  11. -- 查看每组学习情况 平均分降序
  12. -- 对平均分不及格
  13. -- 查看公司每个部门 有几个员工 部门平均工资
  14. select d.deptName, d.deptTel, count(*), avg(salary)
  15. from department d, employee e
  16. where d.departmentCode = e.deparmentCode
  17. group by d.deptName, d.deptTel;
  18. -- 京东 哪些种类的商品 销售量较好
  19. -- kind kindNO kindName
  20. -- product proNo proName price info storeNum salesNum kindNo
  21. select k.kindNo, k.kindName, sum(salesNum)
  22. from kind k, product p
  23. where k.kindNo = p.kindNo
  24. group by k.kindName, k.kindNo
  25. order by sum(salesNum) desc;


2014年11月22日 11:47:35

DQL_数据查询语言的更多相关文章

  1. 数据查询语言DQL 与 内置函数(聚合函数)

    数据查询语言DQL 从表中获取符合条件的数据 select select*from表的名字   查询表所有的数据.(select跟from必须一块用 成对出现的) * 表示所有字段,可以换成想要查询的 ...

  2. 学习资料 数据查询语言DQL

    数据查询语言DQL介绍及其应用: 查询是SQL语言的核心,SQL语言只提供唯一一个用于数据库查询的语句,即SELECT语句.用于表达SQL查询的SELECT语句是功能最强也是最复杂的SQL语句,它提供 ...

  3. SQL 复习二(数据查询语言)

    1.1 数据查询语言 DQL就是数据查询语言,数据库执行DQL语句不会对数据进行改变,而是让数据库发送结果集给客户端. 语法: SELECT selection_list /*要查询的列名称*/ FR ...

  4. MySQL数据库笔记三:数据查询语言(DQL)与事务控制语言(TCL)

    五.数据查询语言(DQL) (重中之重) 完整语法格式: select 表达式1|字段,.... [from 表名 where 条件] [group by 列名] [having 条件] [order ...

  5. 微软亚洲研究院开源图数据查询语言LIKQ

    ​ 近日,微软亚洲研究院通过GitHub 平台开源图数据查询语言LIKQ (Language-Integrated Knowledge Query).LIKQ是基于分布式大规模图数据处理引擎Graph ...

  6. MySQL之数据查询语言(DQL)

    数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHERE子句组成的查询块: SELECT <字段> FROM <表名> WHERE <查询条件> - ...

  7. mysql数据查询语言DQL

    DB(database)数据库:存储数据的'仓库',保存了一系列有组织的数据 DBMS(Database Management System)数据库管理系统:用于创建或管理DB SQL(Structu ...

  8. MySQL — 数据查询语言

    目录 1.基础查询 2.条件查询 3.分组查询 4.排序查询 5.分页查询 6.多表查询 6.1.连接查询 6.1.1.内连接 6.1.2.外连接 6.1.3.自连接 6.1.4.联合查询 6.2.子 ...

  9. 八:SQL之DQL数据查询语言单表操作

    前言: DQL数据库查询语言是我们在开发中最常使用的SQL,这一章总结了单表操作部分的常用查询方式 主要操作有:查询所有字段.查询指定字段.查询指定记录.带IN的关键字查询,范围查询,陪查询.查询空值 ...

随机推荐

  1. memset用法详解(转)

    问题描述: int * cow = new int[n+1]; memset(cow,0,(n+1)*4); 将N+1个元素置成0,为什么不行 memset是对一个字节的int4个字节,因此*4 但是 ...

  2. 超级强大的vim配置(vimplus)

    vimplus vimplus是vim的超级配置安装程序 github地址:https://github.com/chxuan/vimplus.git,欢迎star和fork. 接触vim到现在也有几 ...

  3. 数据结构笔记02:Java面试必问算法题

    1. 面试的时候,栈和队列经常会成对出现来考察.本文包含栈和队列的如下考试内容: (1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min()的 ...

  4. javascript 通过面向对象编写圆形数字时钟

    效果如图所示,代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  5. SSIS 学习(9):包部署常见问题汇总【转】

    Integration Services 包在部署过程中,经常会出现这样那样的问题,让人摸不着头脑,很是烦人.下面我就将我在部署过程中,遇到的一些问题整理出来,以供大家参考. (一)SSIS包在SQL ...

  6. 关于win7系统中所有exe文件都被以word方式打开的解决方法

    手残一刻,电脑桌面所有的软件快捷方式都变成了word的打开方式,鼠标右键选中某exe文件也没打开方式那个选项, 第一次尝试: 在控制面板——默认程序中修改默认打开方式,但是没有找到解决方法

  7. poj 1904 强连通分量

    思路:先有每个儿子向所有他喜欢的姑娘建边,对于最后给出的正确匹配,我们建由姑娘到相应王子的边.和某个王子在同一强连通分量,且王子喜欢的姑娘都是该王子能娶得.思想类似匈牙利算法求匹配的时候,总能找到增广 ...

  8. Codeforces Round #191 (Div. 2)

    在div 188中,幸运的达成了这学期的一个目标:CF1800+,所以这次可以打星去做div2,有点爽. A.Flipping Game 直接枚举 B. Hungry Sequence 由于素数的分布 ...

  9. poj 3544 Journey with Pigs

    Journey with Pigs Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3004   Accepted: 922 ...

  10. 【BZOJ1212】[HNOI2004]L语言 Trie树

    [BZOJ1212][HNOI2004]L语言 Description 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构 ...