标签:2-2 爱情 and gen char res primary prim group
练习脚本:CREATE DATABASE filmclub; USE filmclub; CREATE TABLE category ( cid INT PRIMARY KEY AUTO_INCREMENT, cname VARCHAR(20) ); CREATE TABLE film ( fid INT PRIMARY KEY AUTO_INCREMENT, fname VARCHAR(20), director VARCHAR(20), #导演 price DECIMAL(10,2), showtime DATE, cid INT, FOREIGN KEY(cid) REFERENCES category(cid) ); CREATE TABLE USER ( uid INT PRIMARY KEY AUTO_INCREMENT, uname VARCHAR(20), birthday DATE, gender ENUM('男','女'), address VARCHAR(20), cellphone CHAR(11) ); CREATE TABLE user_category ( uid INT, cid INT, FOREIGN KEY(uid) REFERENCES USER(uid), FOREIGN KEY(cid) REFERENCES category(cid), PRIMARY KEY(uid,cid) ); CREATE TABLE emp ( eid INT PRIMARY KEY AUTO_INCREMENT, ename VARCHAR(20), gender ENUM('男','女'), hiredate DATE, sal DECIMAL(10,2), address VARCHAR(20) ); CREATE TABLE sal_grade ( gid INT PRIMARY KEY AUTO_INCREMENT, minsal DECIMAL(10,2), maxsal DECIMAL(10,2) ); CREATE TABLE orders ( eid INT, uid INT, fid INT, num INT, odate DATE ); INSERT INTO category VALUES(NULL, '喜剧'); INSERT INTO category VALUES(NULL, '动作'); INSERT INTO category VALUES(NULL, '悬疑'); INSERT INTO category VALUES(NULL, '恐怖'); INSERT INTO category VALUES(NULL, '科幻'); INSERT INTO category VALUES(NULL, '战争'); INSERT INTO category VALUES(NULL, '爱情'); INSERT INTO category VALUES(NULL, '灾难'); INSERT INTO film VALUES(NULL, '天下无贼','冯小刚',50,'2008-12-12',1); INSERT INTO film VALUES(NULL, '功夫','周星驰',150,'2009-12-12',2); INSERT INTO film VALUES(NULL, '大话西游','周星驰',20,'2012-3-12',3); INSERT INTO film VALUES(NULL, '我不是潘金莲','冯小刚',30,'2007-5-31',1); INSERT INTO film VALUES(NULL, '道士下山','陈凯歌',40,'2004-8-9',8); INSERT INTO film VALUES(NULL, '火锅英雄','陈凯歌',60,'2011-11-11',7); INSERT INTO film VALUES(NULL, '寻龙诀','冯小刚',100,'2007-7-7',7); INSERT INTO film VALUES(NULL, '老炮儿','陈凯歌',80,'2005-9-2',1); INSERT INTO film VALUES(NULL, '我是证人','周星驰',90,'2010-10-5',2); INSERT INTO film VALUES(NULL, '叶问','冯小刚',120,'2012-6-3',3); INSERT INTO USER VALUES(NULL,'刘欢','1950-1-1','男','北大街','13312345678'); INSERT INTO USER VALUES(NULL,'张学友','1955-2-3','男','南大街','13312345676'); INSERT INTO USER VALUES(NULL,'刘嘉玲','1970-11-21','女','北大街','13312345675'); INSERT INTO USER VALUES(NULL,'李嘉欣','1988-9-3','女','南大街','13312345673'); INSERT INTO USER VALUES(NULL,'刘德华','1953-2-11','男','北大街','13312345672'); INSERT INTO USER VALUES(NULL,'张国立','1999-12-31','男','东大街','13312345671'); INSERT INTO USER VALUES(NULL,'张国荣','1988-3-23','男','西大街','13312345670'); INSERT INTO USER VALUES(NULL,'刘建国','1970-6-22','男','西大街','13312345679'); INSERT INTO user_category VALUES(1,2); INSERT INTO user_category VALUES(1,3); INSERT INTO user_category VALUES(2,8); INSERT INTO user_category VALUES(3,1); INSERT INTO user_category VALUES(3,5); INSERT INTO user_category VALUES(3,7); INSERT INTO user_category VALUES(2,1); INSERT INTO user_category VALUES(1,7); INSERT INTO user_category VALUES(8,8); INSERT INTO user_category VALUES(8,7); INSERT INTO user_category VALUES(5,7); INSERT INTO user_category VALUES(5,3); INSERT INTO user_category VALUES(2,6); INSERT INTO user_category VALUES(7,1); INSERT INTO user_category VALUES(7,2); INSERT INTO user_category VALUES(7,3); INSERT INTO emp VALUES(NULL, '郭靖', '男', '2002-2-3',2500,'东大街'); INSERT INTO emp VALUES(NULL, '黄蓉', '女', '2003-12-3',5500,'东大街'); INSERT INTO emp VALUES(NULL, '杨幂', '女', '2002-2-8',8500,'西大街'); INSERT INTO emp VALUES(NULL, '刘诗诗', '女', '2004-4-12',6500,'南大街'); INSERT INTO sal_grade VALUES(NULL, 1000, 2000); INSERT INTO sal_grade VALUES(NULL, 2001, 4000); INSERT INTO sal_grade VALUES(NULL, 4001, 5000); INSERT INTO sal_grade VALUES(NULL, 5001, 7000); INSERT INTO sal_grade VALUES(NULL, 7001, 9000); INSERT INTO orders VALUES(1,2,10,1,'2016-11-11'); INSERT INTO orders VALUES(2,3,8,2,'2016-2-21'); INSERT INTO orders VALUES(3,7,10,1,'2016-3-21'); INSERT INTO orders VALUES(1,1,7,1,'2016-10-15'); INSERT INTO orders VALUES(1,8,3,1,'2016-2-17'); INSERT INTO orders VALUES(1,7,1,1,'2016-5-18'); INSERT INTO orders VALUES(4,1,1,1,'2016-5-7'); INSERT INTO orders VALUES(4,2,1,1,'2016-5-9'); INSERT INTO orders VALUES(4,3,1,1,'2016-11-10'); INSERT INTO orders VALUES(3,5,10,1,'2016-5-11'); INSERT INTO orders VALUES(2,1,1,1,'2016-7-12'); INSERT INTO orders VALUES(2,1,1,2,'2016-7-13'); INSERT INTO orders VALUES(2,1,1,3,'2016-7-14'); INSERT INTO orders VALUES(2,1,9,5,'2016-8-19'); INSERT INTO orders VALUES(2,1,8,3,'2016-8-4'); INSERT INTO orders VALUES(2,2,5,1,'2016-8-6'); INSERT INTO orders VALUES(2,2,6,1,'2016-1-22'); INSERT INTO orders VALUES(2,2,1,1,'2016-1-11'); INSERT INTO orders VALUES(2,6,3,1,'2016-2-17'); INSERT INTO orders VALUES(2,6,8,2,'2016-3-12'); SELECT * FROM category; SELECT * FROM film; SELECT * FROM USER; SELECT * FROM USER_category; SELECT * FROM emp; SELECT * FROM sal_grade; SELECT * FROM orders;
习题:
#1查询价格比2号类型所有电影平均价还低的电影 SELECT * FROM film WHERE price < (SELECT AVG(price) FROM film WHERE cid=2); SELECT * FROM film; #2查询张学友喜欢的类型中所有电影的最高价 SELECT MAX(price) FROM film WHERE cid IN ( SELECT cid FROM user_category WHERE uid = ( SELECT uid FROM USER WHERE uname = '张学友' ) ); #3查询所有电影,以及电影对应的类型名,要求显示出所有类型名(mysql没有完全外连接FULL JOIN,只能用联合查询) SELECT * FROM film RIGHT JOIN category ON film.cid = category.cid UNION SELECT * FROM film LEFT JOIN category ON film.cid = category.cid #4查询价格大于天下无贼的电影中,每部类型各多少电影 SELECT cid,COUNT(*) FROM film WHERE price > (SELECT price FROM film WHERE fname = '天下无贼') GROUP BY cid; #5查询那些员工给那些会员卖了哪些电影 SELECT ename,uname,fname FROM emp e,USER u,film f,orders o WHERE e.eid = o.eid AND o.uid = u.uid AND f.fid = o.fid #6查询有回头客的员工 SELECT eid,uid,COUNT(*) FROM orders GROUP BY eid,uid HAVING COUNT(*) > 1; #7查询回头客超过一个的员工(表子查询在mysql中必须起别名,否则报错,orcale中则不需要别名) SELECT eid,COUNT(*) FROM ( SELECT eid,uid,COUNT(*) FROM orders GROUP BY eid,uid HAVING COUNT(*) > 1 )aa GROUP BY eid #8查询价格比所有喜剧类型的平均价还高的电影 SELECT * FROM film WHERE price > ( SELECT AVG(price) FROM film WHERE cid = (SELECT cid FROM category WHERE cname = '喜剧') ) #9查询电影所属类型的平均价(相关子查询) SELECT fname,(SELECT AVG(price) FROM film WHERE cid = a.cid) AS 所属类型平均价 FROM film a #10查询价格比1号类型所有电影价格都高的电影 SELECT * FROM film WHERE price > ( SELECT MAX(price) FROM film WHERE cid = 1 ) #11查询上映日期比天下无贼晚的电影中,每个导演各多少部电影 SELECT * FROM film WHERE showtime > (SELECT showtime FROM film WHERE fname = '天下无贼') #12查询哪些用户都喜欢哪些类型 SELECT uname,cname FROM USER,category,user_category WHERE user.uid = user_category.uid AND category.cid = user_category.cid #13查询喜欢类型超过2种的用户 SELECT uname FROM ( SELECT uname,cname FROM USER,category,user_category WHERE user.uid = user_category.uid AND category.cid = user_category.cid )aa GROUP BY uname HAVING COUNT(*) > 2 #14查询从来没有被喜欢过的类型 SELECT cname FROM category WHERE cid NOT IN( SELECT DISTINCT cid FROM user_category) #15查询喜欢了所有类型的用户 SELECT uid,COUNT(*) FROM user_category GROUP BY uid HAVING COUNT(*) = ( SELECT COUNT(*) FROM category ) #16查询每个销售人员的销售总额 SELECT ename,SUM(num*price) FROM emp,film,orders WHERE emp.eid = orders.eid AND orders.fid = film.fid GROUP BY ename #17查询每个电影各被卖出多少张 SELECT fname,SUM(num) FROM film JOIN orders ON film.fid = orders.fid GROUP BY fname #18查询上映日期比周星驰导演所有电影都晚的电影中,每种类型电影的最低价 SELECT (SELECT cname FROM category WHERE a.cid = cid),MIN(price) FROM film a WHERE showtime > (SELECT MAX(showtime) FROM film WHERE director = '周星驰') GROUP BY cid #19查询在闰年上映的电影 SELECT * FROM film WHERE YEAR(showtime) % 4 = 0 AND YEAR(showtime) % 100 != 0 OR YEAR(showtime) % 400 = 0 #20查询上映日期中,月份比日期大的电影 SELECT * FROM film WHERE MONTH(showtime) > DAY(showtime) #21查询价格与类型都与‘天下无贼’相同的电影 SELECT * FROM film WHERE (cid,price) = (SELECT cid,price FROM film WHERE fname = '天下无贼') #22查询最受欢迎的类型 SELECT cid,COUNT(*) FROM user_category GROUP BY cid HAVING COUNT(*) >= ALL (SELECT COUNT(*) FROM user_category GROUP BY cid) #23查询每个用户各花了多少钱 SELECT uid,SUM(num*price) FROM orders,film WHERE orders.fid = film.fid GROUP BY uid
标签:2-2 爱情 and gen char res primary prim group
原文地址:http://blog.51cto.com/12222886/2066830