createview search_tickets as select * from timetable naturaljoin flight naturaljoin airline
dropview xxxx
查找
当输出的属性不为主键时,可能出现重复
LIKE/NOT LIKE匹配
% 代表任意字符串
_ 代表一个字符
SELECTname FROM drinkers WHERE phone LIKE'555%';
-- 查找所有李姓员工 SELECT * FROM employee WHERE empname LIKE'李%';
NULL空值
任意字符串(%)与空值比较的结果是unknown,不会返回unknown结果值
true=1,false=0,unknown=1/2
-- 查找所有job非空的元组 SELECT * FROM workson WHERE job LIKE'%'; -- 或者为 job IS NOT NULL
ORDER BY 输出排序
ORDER BY <list of attributes> ASC(升序,默认),DESC(降序)
SELECT enpname FROM employee WHERE deptno='d2' ORDERBY empno DESC; -- 可以按照不输出的属性排序
多表查询
笛卡尔积
-- 使用笛卡尔积连接两个表 -- 两个表中有同名属性,select其中一个时需要标明,不然会出现两列 SELECT beer FROM frequents,likes WHERE bar='Joe"s Bar'AND frequents.drinker = likes.drinker; -- 待查找的内容为'->"
natural join
只使用于两个表有一列相同属性
SELECT * FROM employee natrual join department WHERE location='天津'; -- 相同的两列不重复出现
-- 等价 SELECT * FROM employee,department WHERE department.deptno=employee.deptno and location='天津'; -- 使用and/or连接多个查询条件
inner join
作用与natural相同,使用与有多个相同属性,使用on进行约束
left/right outer join 左/右外连接
(显示count=0的部分->单元内容为null)
-- 给出每个专业借阅的“c2”类的图书总数,没有借阅的次数显示为0( select majorid,majorname,count(catid) as borrowcount from student leftouterjoin borrow on student.stuid=borrow.stuid leftouterjoin book on book.bookid=borrow.bookid and book.catid='c2' naturaljoin major groupby majorid;
元组变量
引入一个关系的两个或更多部分
-- 返回制造商一致的啤酒对 SELECT b1.name,b2.name FROM beers b1,beers b2 WHERE b1.manf=b2.manf and b1.name<b2.name; -- 避免冗余
子查询
嵌套
-- find bars that serve Miller at the same price Joe charges for Bud SELECT bar FROM sells WHERE beer='Miller'and price= (SELECT price FROM sells WHERE bar='Joe"s Bar'and beer='Bud');
IN/NOT IN操作
只能用于连接查询结果,而不能是 in+<表>
SELECT * FROM Beers WHEREnameIN (SELECT beer FROM likes WHERE drinker='Fred'); -- 当子查询只有一个查询结果,in可改为= -- 或写成 SELECTname,manf FROM Beers,Likes WHERE drinker='Fred'and beer=name;
EXISTS
当且仅当关系非空时为真
-- 不存在制造商一致的不同啤酒-> 一个manf只有一个beer的beer SELECTname FROM beers b1 WHERENOTEXISTS (SELECT * FROM beers WHERE manf = b1.manf and name <> b1.name);
-- 查找部门中只有一名员工的员工姓名 SELECT empname FROM empoyee e1 WHEREnotexists (SELECT * FROM employee WHERE deptno=e1.deptno and empname<>e1.empname);
-- 找到售价最高的,注意是>= SELECT beer FROM sells WHERE price>=ALL(SELECT price FROM sells);
-- 不是最低价格 SELECT beer FROM sells WHERE price>ANY(SELECT price FROM sells);
子表命名
-- 给出借阅了所有图书类别的本科生 select stuid,stuname from (select stuid,count(distinct catid) as times from borrow naturaljoin student naturaljoin book where student.degree='本科生' groupby stuid) as temp1 naturaljoin student where times=(selectcount(*) fromcategory); -- ----------------- select stuid, stuname from student where degree = '本科生'andnotexists( select catid fromcategory where catid notin( select catid from book, borrow where book.bookid = borrow.bookid and borrow.stuid = student.stuid );
Union, Intersecion, Difference
intersect:交集 union:并集 except:A-B
-- 两个集合的交集,要求两个表具有相同的属性列 (SELECT * FROM likes) -- likes(drinker,beer) INTERSECT (SELECT drinker,beer FROM sells,frequents WHERE frequents.bar=sells.bar);
SELECTDISTINCT e1.empname FROM empname e1,empname e2; -- 返回7条
Aggregations聚合操作
sum、avg、min、max、count、count(*)
聚合不可以嵌套使用
AVG
SELECTAVG(price) FROM sells;
COUNT
count忽略null,计算重复 count(*)不忽略null
SELECTCOUNT(DISTINCT price) -- 有多少种不同的售价 FROM sells WHERE beer='Bud';
GROUP BY
分组运算在积运算和投影运算之后进行
SELECT beer,AVG(price) FROM sells GROUPBY beer; -- 获得每种啤酒的平均价格
-- 每个员工做几个项目 SELECT empno,count(*) FROM workson GROUPBY empno;
-- 每个项目由几名员工完成 SELECT projectno,count(*) FROM workson GROUPBY projectno;
NULL单独作为一个分组
-- 1998.04.15后入职员工所参与项目的平均预算 SELECT empno,avg(budget) FROM workson,project WHERE workson.projectno=project.projectno anddate>'1998-04-15' GROUPBY empno;
MIN
SELECTmin(budget) FROMproject;
-- 查找bud的最低价格,不能得到正确结果 SELECT bar,min(price) FROM sells WHERE beer='Bud'; -- 应该写为 SELECT bar FROM sells WHERE beer='Bud'and price<=ALL(SELECT price FROM sells WHERE beer='Bud');
Having
SELECT beer,AVG(price) FROM sells GROUPBY beer -- having 添加group条件,至少在三家酒吧售卖或由B公司制造 HAVINGCOUNT(*)>=3OR--针对group by中的属性 beer IN (SELECTname -- 只有group by中的属性,才能以非聚合形式出现在having中 FROM beers WHERE manf='Busch');