DQL_数据查询语言
2014年11月21日 21:43:53
DQL
基础查询-- 注意要点:1.用户友善的标题
例: select stuno as 学号 , studentname as 姓名 from student;
2.当我们要查询学生来自于哪些城市,出现重复数据
查询非重复数据
我们使用关键字:distinct
例 : select distinct city from city ;
select * from student; -- 所有列 大小写不区分-关键字、列名、表名select * from course;select stuNo, name, sex, address from student;--用户友善的列标题 列名 as "列标题"select stuNo as "学号", name as 姓名, sex 性别, address "地 址" from student;insert into student values('S9008', '张溜溜', null, null, null, null, null, 'M0001', null, '无锡');-- 查询学生来自于哪些城市-- 出现数据重复 查询非重复数据 distinctselect distinct city from student;--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
-- 查询徐州学生select * from student where city = '徐州'-- 逻辑运算符 and or notselect * from student where not city = '徐州' -- city != '徐州'select * from student where city = '徐州' or city = '无锡'-- 比较运算符-- 徐州 考试分数及格学生select * from student where city = '徐州' and javasescore >= 60 and sex = '女'-- 不及格学生select * from student where javasescore < 60select * from users where userName = 'admin' -- 判断用户名是否存在select password from users where userName = 'admin' -- 用户查找密码 - 输入密码比较--select * from users where userName = 'admin' and password = 'admin'-- 查询商品select * from product where kind = '电子' and skind = '手机' and brand = '手机'and size >= 4.7 and price >= 2000 and price <= 3000 andstyle = '4G'-- 其它运算符 between..and.. in(100,200,300)select * from student where javasescore >= 98 and javasescore <= 100select * from student where javasescore between 98 and 100; -- >= <=select * from student where city = '徐州' or city = '无锡'select * from student where city in('徐州', '无锡', '淮北', '南京') -- or-- 算术运算符-- S0001学生参与活动 考试成绩 + 20select stuNo, name, javasescore + 20 as score from student where stuNo = 'S0001';select pname, pprice * 0.5 from product;select empNo, empName, (salary * 0.2 + salary) as newSalary from employee;-- 并置运算符 || - String +select '学号: '||stuNo || ' 姓名: ' ||name || ' 城市: ' || city as stuInfo from student;-- SQL日期默认结构 10-10月-00 2000-10-10select * from student where birthdate = '10-10月-00'; -- to_date to_char
模糊查询
关键字: like
通配符: %
注意点: 1. %在哪边 哪边就有可能有字符,也可以没有
2. 当要查询的字符串中有 _ (下划线) 或者 %(百分号)
需要使用:escape 声明出来 并且 需要使用 反斜杠 转义
例: select * from student where address like '%\_%' escape '\';
select * from student where address like '%无锡%' -- 无锡% %无锡 %无%锡%select * from student where address like '无锡%'select * from student where address like '%无锡%18号%'select * from school where schoolname like '中国%大学'select * from school where schoolname like '中国__大学'-- 信息中包含 % _ 转义select * from student where address like '%\_%' escape '\'select * from student where address like '%\%%' escape '\'
检索遗漏值数据
关键字: is [not] null--- 是否为空
-- 未参与考试学1生select * from student where javasescore is null;select * from student where cardNo is not null;
排序
关键字: order by [要排序的列] desc(降序) asc(升序)
注意要点: 1.如果order by [要排序的列] 之后不加升序还是降序关键字 默认是升序
2.order by 列名 [asc]|desc, 列名 [asc]|desc ----- 表示 : 先按照第一个列名排序
如果相同 ,就按照第二个列名排序
-- 参与考试学生成绩 从上之下排序select stuno, name, javasescore from studentwhere javasescore is not null and javasescore >= 60order by javasescore, stuno desc; -- javasescore升序 相同按stuNo降序排序
多链表查询
- 等值连接:连接条件是等值唯一的 可以确定的(例:两个表之间的外键、主键之间一一对应相等......) ----通常使用 = 符号连接
- 非等值连接 : 连接条件是一个区间范围 ---- 通常使用 between ...and.... 和 比较符号(< >=)来连接
- 外连接 : 当我们等值连接,某端数据多出后(例:要查学生和学生证的信息,但是如果有的学生没有学生证,则输出不了该学生的信息) ---- 通常使用 在等值连接语句的一边加上 (+) 注意:不可以两边都加(+) 【ps:使用规律 :我们给等值语句的两边中少的一方添加该关键符号,比如上例中 没有学生证的学生是多出来的一方,就需要给学生证后面加上关键符号】 或者 使用 join on语句时,在join的前面加上 left || right || full【ps:使用规律 :当使用right或left时 我们要使其方向指向数据多出的一段,上例中应指向student】
- 自连接 : 表中列引用自身作为外键FK
- 自然连接:把列名相同作为公共条件 关键字 from [table1] nature join [table2] (相同列的列名)
- 交叉连接 : 无条件 笛卡尔积 关键字 from [table1] cross join [table2]
代码:
EQUIJOIN(等值联接) -- FK select s.stuno, s.name, m.majorname from student s, major m -- 笛卡尔积 行*行 where s.majorNo = m.majorNo and s.stuno = 'S0001'; -- join tableName on 公共条件 select s.stuno, s.name, m.majorname from student s join major m on s.majorno = m.majorno --and s.stuno = 'S0001'; where s.stuno = 'S0001'; -- 中国大学 有哪些学生 select s.schoolname, f.fname, m.majorname, st.stuno, st.name from school s, student st, faculty f, major m where s.schoolName = '中国大学' and s.schoolcode = f.schoolcode and f.facultyno = m.facultyno and m.majorno = st.majorno; select s.schoolname, f.fname, m.majorname, st.stuno, st.name from school s join faculty f on s.schoolcode = f.schoolcode join major m on f.facultyno = m.facultyno join student st on m.majorno = st.majorno where s.schoolname = '中国大学'; -- 学生选择哪些课程 NONEQUIJOIN(非等值联接) -- 范围 select s.stuno, s.name, s.score, g.gradename, g.money from student s, grade g where s.score between g.minScore and g.maxScore and s.javasescore >= 60; --考试成绩评级 OUTER JOIN (外联接) -- 等值联接 某端数据多出 -- 所有学生及其学生证信息 select * from student s, studentcard c --where s.cardno = c.cardno -- 值与值存在相同 where s.cardno = c.cardno(+); select * from studentcard s right join student c -- left|right|full [outer] join tableName on .. on s.cardno = c.cardno; -- 查看所有学生及其学生会小组信息 学生未参与学生会小组 select * from student s, studentunion n where s.unionno = n.unionno(+); -- 查看所有学会会小组信息 及其参与学生信息 select * from student s, studentunion n where s.unionno(+) = n.unionno; -- 查询学校 所有学生会小组 所有学生的信息 select * from student s, studentunion n where s.unionno = n.unionno(+) union select * from student s, studentunion n where s.unionno(+) = n.unionno; select * from student s full join studentunion n on s.unionno = n.unionno; -- BBS 帖子Post 回复Reply 查询所有帖子及其回复的信息 SELF JOIN (自联接) -- 表中列引用自身列作为FK select s.stuNo, s.name, t.name from student s, student t where t.stuno = s.monitorNo; NATURAL JOIN (自然联接) -- 把列名相同作为公共条件 select * from student s NATURAL join major m; select * from student s join major m using(majorNo); -- on s.majorNo = m.majorNo CROSS JOIN (交叉联接) -- 无条件 笛卡尔积 select * from student CROSS join major;
EQUIJOIN(等值联接) -- FKselect s.stuno, s.name, m.majornamefrom student s, major m -- 笛卡尔积 行*行where s.majorNo = m.majorNo and s.stuno = 'S0001';-- join tableName on 公共条件select s.stuno, s.name, m.majornamefrom student s join major mon s.majorno = m.majorno--and s.stuno = 'S0001';where s.stuno = 'S0001';-- 中国大学 有哪些学生select s.schoolname, f.fname, m.majorname, st.stuno, st.namefrom school s, student st, faculty f, major mwhere s.schoolName = '中国大学' ands.schoolcode = f.schoolcode andf.facultyno = m.facultyno andm.majorno = st.majorno;select s.schoolname, f.fname, m.majorname, st.stuno, st.namefrom school s join faculty f on s.schoolcode = f.schoolcodejoin major m on f.facultyno = m.facultynojoin student st on m.majorno = st.majornowhere s.schoolname = '中国大学';-- 学生选择哪些课程NONEQUIJOIN(非等值联接) -- 范围select s.stuno, s.name, s.score, g.gradename, g.moneyfrom student s, grade gwhere s.score between g.minScore and g.maxScoreand s.javasescore >= 60;--考试成绩评级OUTER JOIN (外联接) -- 等值联接 某端数据多出-- 所有学生及其学生证信息select *from student s, studentcard c--where s.cardno = c.cardno -- 值与值存在相同where s.cardno = c.cardno(+);select *from studentcard s right join student c -- left|right|full [outer] join tableName on ..on s.cardno = c.cardno;-- 查看所有学生及其学生会小组信息 学生未参与学生会小组select *from student s, studentunion nwhere s.unionno = n.unionno(+);-- 查看所有学会会小组信息 及其参与学生信息select *from student s, studentunion nwhere s.unionno(+) = n.unionno;-- 查询学校 所有学生会小组 所有学生的信息select *from student s, studentunion nwhere s.unionno = n.unionno(+)unionselect *from student s, studentunion nwhere s.unionno(+) = n.unionno;select *from student s full join studentunion non s.unionno = n.unionno;-- BBS 帖子Post 回复Reply 查询所有帖子及其回复的信息SELF JOIN (自联接) -- 表中列引用自身列作为FKselect s.stuNo, s.name, t.namefrom student s, student twhere t.stuno = s.monitorNo;NATURAL JOIN (自然联接) -- 把列名相同作为公共条件select *from student s NATURAL join major m;select *from student s join major m using(majorNo); -- on s.majorNo = m.majorNoCROSS JOIN (交叉联接) -- 无条件 笛卡尔积select *from student CROSS join major;
子查询
- 单行子查询 单行子查询 一般使用 = 号
- 多行子查询 多行子查询 一般使用 in (1. 因为子查询结果可能有多个 若使用=号 会因为返回结果多,而报错,使用in 会返回所有结果 2.若比较范围是number类型的值 那么需要使用>any <any >all <all)其中>any 是指大于子查询结果中的任意一个值,等价于最小值 >all指的是大于子查询结果中的所有值,等价于大于最大值。
【ANY】
“比任意一个销售员工资低”==“比最高销售员工资低”;
“比任意一个销售员工资高”==“比最低销售员工资高”;
【ALL】
“比所有销售员工资都低”==“比最低销售员工资低”;
“比所有销售员工资都高”==“比最高销售员工资高”;
--单行子查询 条件 PK UK-- 学校名 中国三美大学 - 学校有哪些院系select s.schoolName, f.fnamefrom school s, faculty fwhere s.schoolCode = f.schoolCodeand s.schoolname = '中国三美大学';-- 给出条件不可直接作为条件操作 - 可通过给定条件查询出所需条件-- 比较运算符 子查询结果必须为单行select f.facultyNo, f.fname -- 院系编号、名称from faculty fwhere f.schoolcode = (select schoolCode from schoolwhere schoolName = '中国三美大学'); -- 子查询 (子查询) 右边-- 中国大学 新开院系 艺术系insert into faculty values('05', '艺术系',(select schoolCode from school where schoolname = '中国大学'));-- 往 体育新闻 增加新新闻稿-- 查看 体育新闻 中新闻稿-- 把市场部员工工资 + 500-- 中国大学 已新开院系 艺术系 加入美声音乐专业insert into major values('M8899', '美声音乐',(select facultyno from faculty where fname = '艺术系'and schoolcode =(select schoolcode from schoolwhere schoolname = '中国大学')));-- BBS 查看Java学习模块 某用户 发的帖子-- 多行子查询-- 不可使用 比较运算符-- 多行运算符in =any-- 报考大学 若有 艺术系 优先考虑select s.schoolcode, s.schoolnamefrom school swhere s.schoolcode in (select schoolCode from facultywhere fname = '艺术系');>any <any -- 数字>all <all-- 查询学生中分数比张三三学生低的学生select s.stuNo, s.name, s.javasescorefrom student swhere s.javasescore <all (select javasescore from studentwhere name = '张三三');
Top - n查询
-- rownum 伪列 默认为数据库中第一行至最后一行 *从1开始*
-- rownum 必须从第一条件开始
-- rownum 可以隐式 不写
-- rownum 必须从第一条件开始select rownum, stuNo, name, score from student where rownum <= 3; -- 获取数据库表中前三条数据-- 成绩前三名select *from (select stuNo, name, score from student order by score desc) -- 虚拟临时表 内联视图where rownum <= 3;
分页数据查询 -
-- rownum as name
-- 当前页面 只显示当前页面数据 - 查询当前页面数据
-- 查询学生及其专业 数据 每个页面显示2条数据select stuNo, name, score, majorNamefrom (select rownum as num, s.stuNo, s.name, s.score, m.name as majorNamefrom student s, major mwhere s.majorno = m.majorno)where num >= 1 and num <= 5;-- mysql select ... limit 1, 5;-- 查询学生及其专业 数据 按学生成绩降序排序 每个页面显示2条数据select stuNo, name, score, majorNamefrom (select rownum as num, stuNo, name, score, majorNamefrom (select s.stuNo, s.name, s.score, m.name as majorNamefrom student s, major mwhere s.majorno = m.majornoorder by s.score desc))where num >= 4 and num <= 6;
group 分组查询
获取组合中数据 - 整体信息
group by ... + 多行函数
注意点:
- 关键词 count(*)或者count(指定的属性)
- group by 后面列出所有的单行函数
- 关键词 avg() 算出平均值
- 在group by 语句完成后 可以跟having avg(.....)来取指定范围的平均值的数据
-- 查看 学校中 每个专业信息 学生数select m.majorno, m.name, count(*), avg(s.javaseScore) as avgScore -- 单行、多行函数结合 必须使用group byfrom major m, student swhere m.majorno = s.majornogroup by m.name, m.majorno -- select中单行函数必须在group by中出现--order by m.majorno desc -- group by中单行函数可不在select中having avg(s.javaseScore) < 80 -- 对分完组数据 条件操作 having 平均分不及格order by avgScore--order by avg(s.javaseScore) desc-- 学生 项目组-- 查看每组学习情况 平均分降序-- 对平均分不及格-- 查看公司每个部门 有几个员工 部门平均工资select d.deptName, d.deptTel, count(*), avg(salary)from department d, employee ewhere d.departmentCode = e.deparmentCodegroup by d.deptName, d.deptTel;-- 京东 哪些种类的商品 销售量较好-- kind kindNO kindName-- product proNo proName price info storeNum salesNum kindNoselect k.kindNo, k.kindName, sum(salesNum)from kind k, product pwhere k.kindNo = p.kindNogroup by k.kindName, k.kindNoorder by sum(salesNum) desc;
2014年11月22日 11:47:35

浙公网安备 33010602011771号