记录我的java之路——day21(周更)

day3-mysql查询与函数

day3-mysql查询与函数查询练习1-企业人事管理系统查询练习2-图书借阅管理mysql常用函数

查询练习1-企业人事管理系统

建表:

--部门表
CREATE TABLE `department` (
  `Depno` int(11) NOT NULL,
  `DepName` varchar(255) DEFAULT NULL,
  `Remark` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`Depno`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
​
--员工表
CREATE TABLE `employee` (
  `Num` int(11) NOT NULL,
  `Name` varchar(255) DEFAULT NULL,
  `Addr` varchar(255) DEFAULT NULL,
  `Zip` varchar(255) DEFAULT NULL,
  `Tel` varchar(255) DEFAULT NULL,
  `Email` varchar(255) DEFAULT NULL,
  `Depno` int(11) DEFAULT NULL,
  `Birth` date DEFAULT NULL,
  `Sex` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`Num`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
​
--收支表
CREATE TABLE `salary` (
  `Num` int(11) NOT NULL,
  `InCome` varchar(255) DEFAULT NULL,
  `OutCome` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`Num`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;

查询:

-- (1)查询每个雇员的所有记录;
select * from employee;
-- (2)查询前5个会员的所有记录;
select * from employee limit 5;
-- (3)查询每个雇员的地址和电话;
select addr,tel from employee;
-- (4)查询num为001的雇员地址和电话;
select addr,tel from employee where num='1';
-- (5)查询表Employee表中女雇员的地址和电话,使用AS子句将结果列中各列的标题分别指定为地址、电话;
select addr as '地址',tel as '电话' from employee where sex='女';
-- (6)计算每个雇员的实际收入;
select income-outcome,num from salary;
-- (7)找出所有性王的雇员的部门号(部门号不能重复显示);
select depno from employee where name like '王%' group by depno;
select distinct depno from employee where name like '王%';
-- (8)找出所有收入在2000-3000元之间的雇员编号
select num from salary where  income between 2000 and 3000;
​
​
-- 练习2:子查询的使用(答案可以不唯一)
-- (1)查找在财务部工作的雇员情况;
-- 子查询
select * from employee where depno=
(select depno from department where DepName='财务部')
-- 等值链接
select e.* from employee e,department d where e.depno=d.depno and d.depname='财务部';
-- 内链接,以两个表共有的数据列为基准查询
select e.* from employee e INNER JOIN department d on e.depno=d.depno and d.depname='财务部';
-- exists
select e.* from employee e where  
exists(select d.* from department d where d.depno=e.depno and d.depname='财务部' );
​
-- (2)查找在财务部且年龄不低于研发部任一个雇员年龄的雇员的姓名;
select e1.name from 
employee e1,
department d,
(select min(e.birth) bir from employee e 
right join department d 
on e.depno=d.depno and d.depname='研发部') e2
where e1.birth < e2.bir and e1.depno=d.depno and d.DepName='财务部'
​
​
-- (3)查找比所有财务部雇员收入都高的雇员的姓名;
select name from employee e1,salary s1 where s1.income>
(select max(s.income) from employee e,department d,salary s where 
e.num = s.num and e.depno=d.depno and d.depname='财务部')
and s1.num=e1.num;
-- exists
select e1.name from employee e1,salary s1 where s1.num=e1.num and not
exists (select * from employee e,department d,salary s where 
e.num = s.num and e.depno=d.depno and d.depname='财务部' and s1.income <= s.income)
​
​
-- 练习3:连接查询的使用
-- (1)查找每个雇员的情况及薪水情况;
select * from employee e,department d,salary s WHERE
e.num=s.num and e.depno=d.depno order by e.num;
​
-- (2)查找财务部收入在2200元以上的雇员姓名及其薪水详细情况;
select e.name,s.* from employee e,department d,salary s WHERE
e.num=s.num and e.depno=d.depno and d.DepName='财务部' and s.InCome>2200;
​
-- 
-- 练习4:数据汇总
-- (1)求财务部雇员的平均实际收入;
select avg(income-outcome) from employee e,department d,salary s
where e.num=s.num and e.depno=d.depno and d.depname='财务部'
​
-- (2)求财务部雇员的总人数;
select count(e.name) from employee e,department d where 
d.depno = e.depno and d.depname='财务部'
​
​
-- 练习5:GROUP  BY 、ORDER BY 子句的使用
-- (1)求各部门的雇员数(要求显示,部门号、部门名称和部门雇员数);
select d.Depno,d.DepName,count(name) from employee e,department d
where d.Depno=e.Depno group by e.depno;
​
-- 'null' <> null
​
-- (2)求部门的平均薪水大于2500的部门信息(要求显示,部门号、部门名称和平均工资)
select d.depno,d.depname,avg(s.income) from employee e,department d,salary s
where e.num = s.num and d.depno=e.depno group by d.depno,d.DepName 
having avg(s.income)>2500

查询练习2-图书借阅管理

建表:

-- ----------------------------
-- Table structure for books
-- ----------------------------
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
  `bno` varchar(10) NOT NULL,
  `bname` varchar(50) NOT NULL,
  `author` varchar(20) NOT NULL,
  `price` double(100,0) DEFAULT NULL,
  `quantity` int(100) DEFAULT NULL,
  PRIMARY KEY (`bno`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
​
-- ----------------------------
-- Records of books
-- ----------------------------
INSERT INTO `books` VALUES ('1001', '九阳真经', '武林', '10000', '2');
INSERT INTO `books` VALUES ('1002', '百花神功', '少林派', '9500', '6');
INSERT INTO `books` VALUES ('1003', '易筋经', '周文王', '9200', '20');
INSERT INTO `books` VALUES ('1004', '葵花宝典', '东方不败', '8880', '0');
INSERT INTO `books` VALUES ('1005', '神照经', '狄云', '7600', '12');
INSERT INTO `books` VALUES ('1006', '九阴真经', '郭襄', '8752', '3');
INSERT INTO `books` VALUES ('1007', '吸星大法', '任千尺', '5212', '2');
INSERT INTO `books` VALUES ('1008', '化功大法', '鬼谷子', '4600', '21');
INSERT INTO `books` VALUES ('1009', '六脉神剑', '段誉', '6325', '6');
INSERT INTO `books` VALUES ('1010', '一阳指', '段正淳', '5300', '11');
​
-- ----------------------------
-- Table structure for borrows
-- ----------------------------
DROP TABLE IF EXISTS `borrows`;
CREATE TABLE `borrows` (
  `no` int(11) NOT NULL AUTO_INCREMENT,
  `cno` varchar(10) NOT NULL,
  `bno` varchar(10) NOT NULL,
  `rdate` datetime DEFAULT NULL,
  `jdate` datetime NOT NULL,
  PRIMARY KEY (`no`),
  KEY `fk_cno` (`cno`),
  KEY `fk_bno` (`bno`),
  CONSTRAINT `fk_bno` FOREIGN KEY (`bno`) REFERENCES `books` (`bno`),
  CONSTRAINT `fk_cno` FOREIGN KEY (`cno`) REFERENCES `card` (`cno`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=gbk;
​
-- ----------------------------
-- Records of borrows
-- ----------------------------
INSERT INTO `borrows` VALUES ('1', '1002', '1001', '2018-07-30 10:34:23', '2018-07-29 11:19:54');
INSERT INTO `borrows` VALUES ('2', '1002', '1002', '2018-07-30 10:34:41', '2018-06-24 11:20:01');
INSERT INTO `borrows` VALUES ('3', '1002', '1003', '2018-07-30 10:35:00', '2018-06-30 11:20:22');
INSERT INTO `borrows` VALUES ('4', '1002', '1004', '2018-07-30 10:35:13', '2018-07-01 12:00:00');
INSERT INTO `borrows` VALUES ('5', '1002', '1005', '2018-08-01 10:35:33', '2018-07-01 12:00:00');
INSERT INTO `borrows` VALUES ('6', '1002', '1006', '2018-08-01 10:35:54', '2018-07-01 12:00:00');
INSERT INTO `borrows` VALUES ('7', '1002', '1007', '2018-08-03 10:36:13', '2018-07-11 12:00:00');
INSERT INTO `borrows` VALUES ('8', '1003', '1005', '2018-07-17 10:36:33', '2018-06-11 12:00:00');
INSERT INTO `borrows` VALUES ('9', '1006', '1006', '2018-07-02 10:36:54', '2018-06-01 12:00:00');
INSERT INTO `borrows` VALUES ('10', '1005', '1008', '2018-07-12 10:37:24', '2018-07-01 12:00:00');
INSERT INTO `borrows` VALUES ('11', '1007', '1006', '2018-07-17 10:37:42', '2018-06-21 12:00:00');
INSERT INTO `borrows` VALUES ('12', '1009', '1008', '2018-08-07 10:38:00', '2018-07-30 11:22:35');
INSERT INTO `borrows` VALUES ('13', '1004', '1006', '2018-08-17 10:38:16', '2018-07-30 11:23:00');
INSERT INTO `borrows` VALUES ('14', '1004', '1004', '2018-08-07 10:38:31', '2018-07-21 00:00:00');
INSERT INTO `borrows` VALUES ('15', '1005', '1001', '2018-06-30 10:38:50', '2018-06-05 00:00:00');
INSERT INTO `borrows` VALUES ('16', '1009', '1003', '2018-07-29 10:39:09', '2018-06-07 00:12:23');
INSERT INTO `borrows` VALUES ('17', '1009', '1006', null, '2018-07-02 11:24:50');
INSERT INTO `borrows` VALUES ('18', '1006', '1009', null, '2018-05-31 11:25:32');
​
-- ----------------------------
-- Table structure for card
-- ----------------------------
DROP TABLE IF EXISTS `card`;
CREATE TABLE `card` (
  `cno` varchar(10) NOT NULL,
  `name` varchar(20) NOT NULL,
  `class` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`cno`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
​
-- ----------------------------
-- Records of card
-- ----------------------------
INSERT INTO `card` VALUES ('1002', '郭靖', '丐帮');
INSERT INTO `card` VALUES ('1003', '黄蓉', '东邪');
INSERT INTO `card` VALUES ('1004', '杨康', '辽国');
INSERT INTO `card` VALUES ('1005', '穆念慈', '辽国');
INSERT INTO `card` VALUES ('1006', '华筝', '蒙古 ');
INSERT INTO `card` VALUES ('1007', '黄药师', '东邪');
INSERT INTO `card` VALUES ('1008', '欧阳峰', '西毒');
INSERT INTO `card` VALUES ('1009', '一灯大师', '南帝');
INSERT INTO `card` VALUES ('1010', '洪七公', '北丐');
INSERT INTO `card` VALUES ('1011', '柯镇恶', '江南七怪');
INSERT INTO `card` VALUES ('1012', '朱聪', '江南七怪');
INSERT INTO `card` VALUES ('1013', '韩宝驹', '江南七怪');
INSERT INTO `card` VALUES ('1014', '南希仁', '江南七怪');
INSERT INTO `card` VALUES ('1015', '张阿生', '江南七怪');
INSERT INTO `card` VALUES ('1016', '全金发', '江南七怪');
INSERT INTO `card` VALUES ('1017', '韩小莹', '江南七怪');

查询:

-- 2.找出借书超过5本的读者,输出借书卡号及所借图书册数。
select cno,count(cno) from borrows group by cno having count(cno)>5;
​
-- 3.查询借阅了"葵花宝典"一书的读者,输出姓名及班级。
select c.name,c.class from books b,borrows br,card c
where br.cno=c.cno and br.bno=b.bno and b.bname='葵花宝典';
​
-- 4.查询过期未还图书,输出借阅者(卡号)、书号及还书日期。
select * from borrows  where rdate 
  

mysql常用函数

 

 

你可能感兴趣的:(记录我的java之路——day21(周更))