使用 DML语句,对 “锦图网” 数据进行操作,聚合函数练习
查看本章节
查看作业目录
需求说明:
- 根据客户 ID 统计订单数、订单总金额、最高订单金额、最低订单金额和每份订单平均金额,并按订单总金额升序显示
- 根据客户统计订单总订购人次数> 5 的统计信息
根据以上要求对 “锦图网” 数据进行操作:
客户表(customer)
-- ----------------------------
-- Table structure for `customer`
-- ----------------------------
DROP TABLE IF EXISTS `customer`;
CREATE TABLE `customer` (
`customerID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`gender` varchar(50) DEFAULT NULL,
`identityID` varchar(18) DEFAULT NULL,
`tel` varchar(18) DEFAULT NULL,
PRIMARY KEY (`customerID`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of customer
-- ----------------------------
INSERT INTO `customer` VALUES ('1', '魏国兰', '女', '420103198309125344', '13923561234');
INSERT INTO `customer` VALUES ('2', '刘亚蒙', '男', '420105197610200916', '13867893421');
INSERT INTO `customer` VALUES ('3', '郝琼琼', '女', '420104198703125881', '15902712563');
INSERT INTO `customer` VALUES ('4', '雷亚波', '男', '420103199806195830', '13686035678');
INSERT INTO `customer` VALUES ('5', '李慧娟', '女', '420106199208113738', '13798235671');
线路类型表(lineType)
-- ----------------------------
-- Table structure for `linetype`
-- ----------------------------
DROP TABLE IF EXISTS `linetype`;
CREATE TABLE `linetype` (
`lineTypeID` int(11) NOT NULL AUTO_INCREMENT,
`typeName` varchar(50) DEFAULT NULL,
PRIMARY KEY (`lineTypeID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of linetype
-- ----------------------------
INSERT INTO `linetype` VALUES ('1', '国内短线游');
INSERT INTO `linetype` VALUES ('2', '国内长线游');
INSERT INTO `linetype` VALUES ('3', '出境游');
线路表(line)
-- ----------------------------
-- Table structure for `line`
-- ----------------------------
DROP TABLE IF EXISTS `line`;
CREATE TABLE `line` (
`lineID` int(11) NOT NULL AUTO_INCREMENT,
`lineTypeID` int(11) DEFAULT NULL,
`lineName` varchar(50) NOT NULL,
`days` int(11) DEFAULT NULL,
`price` decimal(10,2) DEFAULT NULL,
`vehicle` char(10) DEFAULT NULL,
`hotel` char(10) DEFAULT NULL,
`hasMeal` char(2) DEFAULT NULL,
PRIMARY KEY (`lineID`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of line
-- ----------------------------
INSERT INTO `line` VALUES ('1', '1', '黄陂木兰天池', '1', '159.00', '大巴', '无', '无');
INSERT INTO `line` VALUES ('2', '1', '大别山天堂寨', '2', '429.00', '大巴', '二星级', '无');
INSERT INTO `line` VALUES ('5', '1', '恩施大峡谷', '4', '1089.00', '火车卧铺', '二星级', '无');
INSERT INTO `line` VALUES ('6', '1', '庐山', '2', '729.00', '大巴', '二星级', '含');
INSERT INTO `line` VALUES ('7', '1', '凤凰古城', '3', '959.00', '火车卧铺', '二星级', '含');
INSERT INTO `line` VALUES ('8', '1', '黄山', '3', '1099.00', '动车', '三星级', '含');
INSERT INTO `line` VALUES ('9', '2', '海南岛三亚', '5', '3868.00', '飞机', '三星级', '含');
INSERT INTO `line` VALUES ('10', '2', '青岛蓬莱', '4', '2680.00', '飞机', '三星级', '含');
INSERT INTO `line` VALUES ('12', '2', '桂林', '5', '1920.00', '火车卧铺', '二星级', '无');
INSERT INTO `line` VALUES ('13', '2', '华东五市', '6', '2856.00', '动车', '三星级', '含');
INSERT INTO `line` VALUES ('14', '2', '成都九寨沟', '7', '4500.00', '飞机', '三星级', '含');
INSERT INTO `line` VALUES ('15', '2', '西安', '4', '2180.00', '动车', '三星级', '无');
INSERT INTO `line` VALUES ('16', '3', '欧洲德法意瑞', '13', '12294.91', '飞机', '四星级', '含');
INSERT INTO `line` VALUES ('17', '3', '日本东京富士山', '6', '7119.09', '飞机', '三星级', '含');
INSERT INTO `line` VALUES ('18', '3', '新马泰', '8', '6058.80', '飞机', '三星级', '含');
INSERT INTO `line` VALUES ('19', '3', '美国夏威夷', '6', '11493.90', '飞机', '四星级', '无');
INSERT INTO `line` VALUES ('20', null, '梁子湖游', '1', '168.00', '大巴', '无', '无');
INSERT INTO `line` VALUES ('21', null, '洪湖游', '1', '128.00', '大巴', '无', '无');
订单表(orders)
-- ----------------------------
-- Table structure for `orders`
-- ----------------------------
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`ordersID` int(11) NOT NULL AUTO_INCREMENT,
`customerID` int(11) DEFAULT NULL,
`ordersDate` date DEFAULT NULL,
`amount` decimal(8,2) DEFAULT NULL,
`man_times` int(11) DEFAULT NULL,
`discount` decimal(8,2) DEFAULT NULL,
`effectiveAmount` decimal(8,2) DEFAULT NULL,
PRIMARY KEY (`ordersID`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of orders
-- ----------------------------
INSERT INTO `orders` VALUES ('1', '1', '2018-10-20', '798.00', '2', '0.98', '782.04');
INSERT INTO `orders` VALUES ('2', '1', '2018-01-15', '1898.00', '2', '0.98', '1860.04');
INSERT INTO `orders` VALUES ('3', '2', '2018-06-18', '2574.00', '6', '0.96', '2471.04');
INSERT INTO `orders` VALUES ('4', '3', '2018-08-21', '2856.00', '1', '1.00', '2856.00');
INSERT INTO `orders` VALUES ('5', '4', '2018-10-10', '7698.00', '4', '0.96', '7390.08');
INSERT INTO `orders` VALUES ('6', '5', '2018-06-23', '6732.00', '1', '1.00', '6732.00');
INSERT INTO `orders` VALUES ('7', '3', '2018-10-11', '4360.00', '2', '0.98', '4272.80');
INSERT INTO `orders` VALUES ('8', '4', '2018-11-21', '12771.00', '1', '1.00', '12771.00');
INSERT INTO `orders` VALUES ('9', '4', '2013-12-20', '899.00', '1', '0.98', '881.02');
订单客户明细表(oc_detail)
-- ----------------------------
-- Table structure for `oc_detail`
-- ----------------------------
DROP TABLE IF EXISTS `oc_detail`;
CREATE TABLE `oc_detail` (
`travelCustomerID` int(11) NOT NULL,
`ordersID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of oc_detail
-- ----------------------------
INSERT INTO `oc_detail` VALUES ('1', '1');
INSERT INTO `oc_detail` VALUES ('1', '2');
INSERT INTO `oc_detail` VALUES ('2', '3');
INSERT INTO `oc_detail` VALUES ('2', '5');
INSERT INTO `oc_detail` VALUES ('2', '7');
INSERT INTO `oc_detail` VALUES ('3', '3');
INSERT INTO `oc_detail` VALUES ('3', '4');
INSERT INTO `oc_detail` VALUES ('3', '7');
INSERT INTO `oc_detail` VALUES ('4', '3');
INSERT INTO `oc_detail` VALUES ('4', '5');
INSERT INTO `oc_detail` VALUES ('4', '8');
INSERT INTO `oc_detail` VALUES ('4', '9');
INSERT INTO `oc_detail` VALUES ('5', '1');
INSERT INTO `oc_detail` VALUES ('5', '6');
订单线路明细表(ol_detail)
-- ----------------------------
-- Table structure for `ol_detail`
-- ----------------------------
DROP TABLE IF EXISTS `ol_detail`;
CREATE TABLE `ol_detail` (
`ordersID` int(11) NOT NULL,
`lineID` int(11) NOT NULL,
`travelDate` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of ol_detail
-- ----------------------------
INSERT INTO `ol_detail` VALUES ('1', '2', '2018-10-27');
INSERT INTO `ol_detail` VALUES ('2', '5', '2018-01-20');
INSERT INTO `ol_detail` VALUES ('2', '7', '2018-02-01');
INSERT INTO `ol_detail` VALUES ('3', '1', '2018-06-26');
INSERT INTO `ol_detail` VALUES ('3', '6', '2018-07-05');
INSERT INTO `ol_detail` VALUES ('4', '13', '2018-08-29');
INSERT INTO `ol_detail` VALUES ('5', '1', '2018-10-16');
INSERT INTO `ol_detail` VALUES ('5', '14', '2018-10-21');
INSERT INTO `ol_detail` VALUES ('6', '18', '2018-07-10');
INSERT INTO `ol_detail` VALUES ('7', '15', '2018-10-19');
INSERT INTO `ol_detail` VALUES ('8', '19', '2018-11-27');
INSERT INTO `ol_detail` VALUES ('9', '7', '2018-12-28');
实现代码:
根据客户 ID 统计订单数、订单总金额、最高订单金额、最低订单金额和每份订单平均金额,并按订单总金额升序显示
select customerID 客户 ID, count(ordersID) 订单数 , sum(amount) 订单总金额 ,max(amount) 最高订单金额 ,min(amount) 最低订单金额 , avg(amount) 每份订单平均金额 from orders group by customerID order by 订单总金额
根据客户统计订单总订购人次数> 5 的统计信息
select customerID 客户 ID, sum(man_times) 总订购人次数 from orders group by customerID having sum(man_times)>5
“锦图网” 完整数据库:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `customer`
-- ----------------------------
DROP TABLE IF EXISTS `customer`;
CREATE TABLE `customer` (
`customerID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`gender` varchar(50) DEFAULT NULL,
`identityID` varchar(18) DEFAULT NULL,
`tel` varchar(18) DEFAULT NULL,
PRIMARY KEY (`customerID`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of customer
-- ----------------------------
INSERT INTO `customer` VALUES ('1', '魏国兰', '女', '420103198309125344', '13923561234');
INSERT INTO `customer` VALUES ('2', '刘亚蒙', '男', '420105197610200916', '13867893421');
INSERT INTO `customer` VALUES ('3', '郝琼琼', '女', '420104198703125881', '15902712563');
INSERT INTO `customer` VALUES ('4', '雷亚波', '男', '420103199806195830', '13686035678');
INSERT INTO `customer` VALUES ('5', '李慧娟', '女', '420106199208113738', '13798235671');
-- ----------------------------
-- Table structure for `line`
-- ----------------------------
DROP TABLE IF EXISTS `line`;
CREATE TABLE `line` (
`lineID` int(11) NOT NULL AUTO_INCREMENT,
`lineTypeID` int(11) DEFAULT NULL,
`lineName` varchar(50) NOT NULL,
`days` int(11) DEFAULT NULL,
`price` decimal(10,2) DEFAULT NULL,
`vehicle` char(10) DEFAULT NULL,
`hotel` char(10) DEFAULT NULL,
`hasMeal` char(2) DEFAULT NULL,
PRIMARY KEY (`lineID`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of line
-- ----------------------------
INSERT INTO `line` VALUES ('1', '1', '黄陂木兰天池', '1', '159.00', '大巴', '无', '无');
INSERT INTO `line` VALUES ('2', '1', '大别山天堂寨', '2', '429.00', '大巴', '二星级', '无');
INSERT INTO `line` VALUES ('5', '1', '恩施大峡谷', '4', '1089.00', '火车卧铺', '二星级', '无');
INSERT INTO `line` VALUES ('6', '1', '庐山', '2', '729.00', '大巴', '二星级', '含');
INSERT INTO `line` VALUES ('7', '1', '凤凰古城', '3', '959.00', '火车卧铺', '二星级', '含');
INSERT INTO `line` VALUES ('8', '1', '黄山', '3', '1099.00', '动车', '三星级', '含');
INSERT INTO `line` VALUES ('9', '2', '海南岛三亚', '5', '3868.00', '飞机', '三星级', '含');
INSERT INTO `line` VALUES ('10', '2', '青岛蓬莱', '4', '2680.00', '飞机', '三星级', '含');
INSERT INTO `line` VALUES ('12', '2', '桂林', '5', '1920.00', '火车卧铺', '二星级', '无');
INSERT INTO `line` VALUES ('13', '2', '华东五市', '6', '2856.00', '动车', '三星级', '含');
INSERT INTO `line` VALUES ('14', '2', '成都九寨沟', '7', '4500.00', '飞机', '三星级', '含');
INSERT INTO `line` VALUES ('15', '2', '西安', '4', '2180.00', '动车', '三星级', '无');
INSERT INTO `line` VALUES ('16', '3', '欧洲德法意瑞', '13', '12294.91', '飞机', '四星级', '含');
INSERT INTO `line` VALUES ('17', '3', '日本东京富士山', '6', '7119.09', '飞机', '三星级', '含');
INSERT INTO `line` VALUES ('18', '3', '新马泰', '8', '6058.80', '飞机', '三星级', '含');
INSERT INTO `line` VALUES ('19', '3', '美国夏威夷', '6', '11493.90', '飞机', '四星级', '无');
INSERT INTO `line` VALUES ('20', null, '梁子湖游', '1', '168.00', '大巴', '无', '无');
INSERT INTO `line` VALUES ('21', null, '洪湖游', '1', '128.00', '大巴', '无', '无');
-- ----------------------------
-- Table structure for `linetype`
-- ----------------------------
DROP TABLE IF EXISTS `linetype`;
CREATE TABLE `linetype` (
`lineTypeID` int(11) NOT NULL AUTO_INCREMENT,
`typeName` varchar(50) DEFAULT NULL,
PRIMARY KEY (`lineTypeID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of linetype
-- ----------------------------
INSERT INTO `linetype` VALUES ('1', '国内短线游');
INSERT INTO `linetype` VALUES ('2', '国内长线游');
INSERT INTO `linetype` VALUES ('3', '出境游');
-- ----------------------------
-- Table structure for `oc_detail`
-- ----------------------------
DROP TABLE IF EXISTS `oc_detail`;
CREATE TABLE `oc_detail` (
`travelCustomerID` int(11) NOT NULL,
`ordersID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of oc_detail
-- ----------------------------
INSERT INTO `oc_detail` VALUES ('1', '1');
INSERT INTO `oc_detail` VALUES ('1', '2');
INSERT INTO `oc_detail` VALUES ('2', '3');
INSERT INTO `oc_detail` VALUES ('2', '5');
INSERT INTO `oc_detail` VALUES ('2', '7');
INSERT INTO `oc_detail` VALUES ('3', '3');
INSERT INTO `oc_detail` VALUES ('3', '4');
INSERT INTO `oc_detail` VALUES ('3', '7');
INSERT INTO `oc_detail` VALUES ('4', '3');
INSERT INTO `oc_detail` VALUES ('4', '5');
INSERT INTO `oc_detail` VALUES ('4', '8');
INSERT INTO `oc_detail` VALUES ('4', '9');
INSERT INTO `oc_detail` VALUES ('5', '1');
INSERT INTO `oc_detail` VALUES ('5', '6');
-- ----------------------------
-- Table structure for `ol_detail`
-- ----------------------------
DROP TABLE IF EXISTS `ol_detail`;
CREATE TABLE `ol_detail` (
`ordersID` int(11) NOT NULL,
`lineID` int(11) NOT NULL,
`travelDate` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of ol_detail
-- ----------------------------
INSERT INTO `ol_detail` VALUES ('1', '2', '2018-10-27');
INSERT INTO `ol_detail` VALUES ('2', '5', '2018-01-20');
INSERT INTO `ol_detail` VALUES ('2', '7', '2018-02-01');
INSERT INTO `ol_detail` VALUES ('3', '1', '2018-06-26');
INSERT INTO `ol_detail` VALUES ('3', '6', '2018-07-05');
INSERT INTO `ol_detail` VALUES ('4', '13', '2018-08-29');
INSERT INTO `ol_detail` VALUES ('5', '1', '2018-10-16');
INSERT INTO `ol_detail` VALUES ('5', '14', '2018-10-21');
INSERT INTO `ol_detail` VALUES ('6', '18', '2018-07-10');
INSERT INTO `ol_detail` VALUES ('7', '15', '2018-10-19');
INSERT INTO `ol_detail` VALUES ('8', '19', '2018-11-27');
INSERT INTO `ol_detail` VALUES ('9', '7', '2018-12-28');
-- ----------------------------
-- Table structure for `orders`
-- ----------------------------
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`ordersID` int(11) NOT NULL AUTO_INCREMENT,
`customerID` int(11) DEFAULT NULL,
`ordersDate` date DEFAULT NULL,
`amount` decimal(8,2) DEFAULT NULL,
`man_times` int(11) DEFAULT NULL,
`discount` decimal(8,2) DEFAULT NULL,
`effectiveAmount` decimal(8,2) DEFAULT NULL,
PRIMARY KEY (`ordersID`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of orders
-- ----------------------------
INSERT INTO `orders` VALUES ('1', '1', '2018-10-20', '798.00', '2', '0.98', '782.04');
INSERT INTO `orders` VALUES ('2', '1', '2018-01-15', '1898.00', '2', '0.98', '1860.04');
INSERT INTO `orders` VALUES ('3', '2', '2018-06-18', '2574.00', '6', '0.96', '2471.04');
INSERT INTO `orders` VALUES ('4', '3', '2018-08-21', '2856.00', '1', '1.00', '2856.00');
INSERT INTO `orders` VALUES ('5', '4', '2018-10-10', '7698.00', '4', '0.96', '7390.08');
INSERT INTO `orders` VALUES ('6', '5', '2018-06-23', '6732.00', '1', '1.00', '6732.00');
INSERT INTO `orders` VALUES ('7', '3', '2018-10-11', '4360.00', '2', '0.98', '4272.80');
INSERT INTO `orders` VALUES ('8', '4', '2018-11-21', '12771.00', '1', '1.00', '12771.00');
INSERT INTO `orders` VALUES ('9', '4', '2013-12-20', '899.00', '1', '0.98', '881.02');
-- ----------------------------
-- View structure for `v_customer_orderline_detail`
-- ----------------------------
DROP VIEW IF EXISTS `v_customer_orderline_detail`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`127.0.0.1` SQL SECURITY DEFINER VIEW `v_customer_orderline_detail` AS select `c`.`name` AS `客户名`,`l`.`lineName` AS `线路名`,`l`.`days` AS `行程天数`,`l`.`price` AS `价格` from (((`customer` `c` join `oc_detail` `ocd`) join `ol_detail` `old`) join `line` `l`) where ((`c`.`customerID` = `ocd`.`travelCustomerID`) and (`ocd`.`ordersID` = `old`.`ordersID`) and (`old`.`lineID` = `l`.`lineID`)) ;
-- ----------------------------
-- View structure for `v_customer_orders_detail`
-- ----------------------------
DROP VIEW IF EXISTS `v_customer_orders_detail`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`127.0.0.1` SQL SECURITY DEFINER VIEW `v_customer_orders_detail` AS select `c`.`name` AS `name`,`l`.`lineName` AS `lineName`,`l`.`days` AS `days`,`l`.`price` AS `price` from (((`customer` `c` join `oc_detail` `ocd`) join `ol_detail` `old`) join `line` `l`) where ((`c`.`customerID` = `ocd`.`travelCustomerID`) and (`ocd`.`ordersID` = `old`.`ordersID`) and (`old`.`lineID` = `l`.`lineID`)) ;
-- ----------------------------
-- View structure for `v_nums_line`
-- ----------------------------
DROP VIEW IF EXISTS `v_nums_line`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`127.0.0.1` SQL SECURITY DEFINER VIEW `v_nums_line` AS select `l`.`lineName` AS `线路`,count(`old`.`lineID`) AS `预订数` from (`line` `l` join `ol_detail` `old`) where (`l`.`lineID` = `old`.`lineID`) group by `l`.`lineName` ;
-- ----------------------------
-- Procedure structure for `proc_adjust_price`
-- ----------------------------
DROP PROCEDURE IF EXISTS `proc_adjust_price`;
DELIMITER ;;
CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `proc_adjust_price`(
out oldPrice decimal, -- 原价
out newPrice decimal, -- 现价
out lineName_maxPrice varchar(50) -- 价格最高国内长线游线路名
)
MODIFIES SQL DATA
begin
declare lineID_maxPrice int; -- 价格最高国内长线游线路编号
select max(price) into oldPrice from line where lineTypeID=
(select lineTypeID from linetype where typeName='国内长线游');
select lineID, lineName into lineID_maxPrice, lineName_maxPrice from line
where price=oldPrice and lineTypeID=(select lineTypeID from linetype where typeName='国内长线游');
if oldPrice<3000 then
set newPrice=oldPrice;
elseif oldPrice>=3000 and oldPrice<4000 then
set newPrice=oldPrice*0.95;
elseif oldPrice>=4000 and oldPrice<5000 then
set newPrice=oldPrice*0.93;
else
set newPrice=oldPrice*0.90;
end if;
if newPrice<>oldPrice then
update line set price=newPrice where lineID=lineID_maxPrice;
end if;
end
;;
DELIMITER ;
-- ----------------------------
-- Procedure structure for `proc_deleteLineType`
-- ----------------------------
DROP PROCEDURE IF EXISTS `proc_deleteLineType`;
DELIMITER ;;
CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `proc_deleteLineType`(
_typeName varchar(20) -- 线路类型名称
)
MODIFIES SQL DATA
begin
declare state varchar(20);
declare _lineTypeID int; -- 线路类型编号
-- 定义错误处理
declare continue handler for sqlexception set state='error';
select lineTypeID into _lineTypeID from LineType where typeName=_typeName;
-- 开启事务
start transaction;
-- 将线路中所需要删除的线路类型的编号置为NULL
update line set lineTypeID=NULL where lineTypeID=_lineTypeID;
if(state='error') then
select '线路信息修改失败';
rollback;
else
delete from LineType where typeName=_typeName;
if(state='error') then
select '线路类型删除失败';
rollback;
else
select '线路类型删除成功';
commit;
end if;
end if;
end
;;
DELIMITER ;
-- ----------------------------
-- Procedure structure for `proc_LineDetail`
-- ----------------------------
DROP PROCEDURE IF EXISTS `proc_LineDetail`;
DELIMITER ;;
CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `proc_LineDetail`(
_lineName varchar(20)
)
READS SQL DATA
begin
select lineName 线路名, days 行程天数, price 价格, vehicle 交通工具, hotel 住宿标准
from line where lineName=_lineName;
end
;;
DELIMITER ;
-- ----------------------------
-- Procedure structure for `proc_LineNumsRate`
-- ----------------------------
DROP PROCEDURE IF EXISTS `proc_LineNumsRate`;
DELIMITER ;;
CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `proc_LineNumsRate`(
_typeName varchar(20),
out numsRate decimal(10,3)
)
READS SQL DATA
begin
declare totalNum int; -- 定义全部线路数
declare num int; -- 定义指定类型的线路数
select count(*) into totalNum from line;
select count(*) into num from line L, linetype LT where L.lineTypeID=LT.lineTypeID
and LT.typeName=_typeName;
-- 生成指定类型的线路数与全部线路数之比,赋给输出参数numsRate
set numsRate=num*1.0/totalNum;
end
;;
DELIMITER ;
-- ----------------------------
-- Procedure structure for `proc_NumsGivenLineType`
-- ----------------------------
DROP PROCEDURE IF EXISTS `proc_NumsGivenLineType`;
DELIMITER ;;
CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `proc_NumsGivenLineType`(
_typeName varchar(20),
out count int -- 输出参数,用于输出指定线路类型的总预订数
)
READS SQL DATA
begin
select count(OLD.lineID) into count from ol_detail OLD, line L, lineType LT
where OLD.lineID=L.lineID and L.lineTypeID=LT.lineTypeID and typeName=_typeName;
end
;;
DELIMITER ;
-- ----------------------------
-- Procedure structure for `proc_PriceModify`
-- ----------------------------
DROP PROCEDURE IF EXISTS `proc_PriceModify`;
DELIMITER ;;
CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `proc_PriceModify`()
MODIFIES SQL DATA
begin
declare _lineID int;
declare _days int; -- 线路旅程天数
declare reduce_money decimal(10,2); -- 减免的住宿费
declare state varchar(20); -- 错误状态
declare line_cursor1 cursor for select lineID, days from line where hotel='二星级';
declare continue handler for 1329 set state='error';
open line_cursor1;
traverse_line:while true do
fetch line_cursor1 into _lineID, _days;
if(state='error') then
leave traverse_line;
end if;
set reduce_money=(_days-1)*30;
update line set price=price-reduce_money where lineID=_lineID;
end while;
close line_cursor1;
end
;;
DELIMITER ;
使用 DML语句,对 “锦图网” 数据进行操作,聚合函数练习的更多相关文章
- 使用 DML语句,对 “锦图网” 数据进行操作,连接查询(内连接,左外连接,右外连接,全连接)
查看本章节 查看作业目录 需求说明: 对 "锦图网" 数据进行操作: 统计每一种线路类型的线路数量.最高线路价格.最低线路价格和平均线路价格,要求按照线路数量和平均线路价格升序显示 ...
- MySql查询语句中解决“该列没有包含在聚合函数或者groupby子句中”的相关问题方法
首先引入语句来源,表结构和数据如下: 需求是:查出员工(personname)在不同店铺(store)的总薪酬(salary),相同店铺输出store,不同店铺输出multi_store. 正确查询语 ...
- SQL 数据库备、还,附、分,数据查询,聚合函数
认识数据库备份和事务日志备份 数据库备份与日志备份是数据库维护的日常工作,备份的目的是在于当数据库出现故障或者遭到破坏时可以根据备份的数据库及事务日志文件还原到最近的时间点将损失降到最低点. 数据库备 ...
- MySQL查询语句详解,排序、分组、聚合函数、约束
create database day20; 查询的时候from前面的字段是需要显示出来的内容,后面是条件use day20;create table phones(id int,pinpai var ...
- 入门大数据---SparkSQL常用聚合函数
一.简单聚合 1.1 数据准备 // 需要导入 spark sql 内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSe ...
- update(修改,DML语句) 和 delete(删除数据,DML语句)
7.7.修改update(DML) 语法格式: update 表名 set 字段名1=值1,字段名2=值2,字段名3=值3....where 条件; 注意:没有条件限制会导致所有数据全部更新 upda ...
- 使用 DML语句针对仓库管理信息系统,进行查询操作
查看本章节 查看作业目录 需求说明: 查询所有电视机产品的基本信息,要求显示产品编号.产品名和进货单价 查询所有产品的基本信息,要求按类型升序.价格降序显示查询信息 显示所有不重复的产品类型 显示进货 ...
- 微软BI 之SSAS 系列 - 多维数据集中度量值设计时的聚合函数 (累加性_半累加性和非累加性)
在 SSAS 系列 - 实现第一个 Cube 以及角色扮演维度,度量值格式化和计算成员的创建 中主要是通过已存在的维度和事实数据创建了一个多维数据集,并同时解释了 Role-Playing Dimen ...
- group by语句,聚合函数与其他语句(关系,执行过程)
(本文一切基于SQL SERVER 2008 R2) 先来看一个语句执行过程发生的错误提示: 消息 8120,级别 16,状态 1,第 2 行 选择列表中的列 'SC.CNO' 无效,因为该列没有包含 ...
随机推荐
- C++ 害死人不偿命的(3n+1)猜想
第一次刷PAT ,注意事项:就像普通编译器一样要导入头文件 还有 using namespace std:要不然会报错(鬼知道我经历了什么 微笑.jpg) 1 #include <iostrea ...
- 最新的Android Sdk 使用Ant多渠道批量打包
实例工程.所需的文件都在最后的附件中. 今天花费了几个小时,参考网上的资料,期间遇到了好几个问题, 终于实现了使用Ant批量多渠道打包,现在,梳理一下思路,总结使用Ant批量多渠道打包的方法:1 ...
- Java Web 实现Mysql 数据库备份与还原
前段时间某某删库事故付出的惨重代价告诉我们: 数据备份的必要性是企业数据管理极其重要的一项工作. 1. Mysql备份与还原命令 备份命令: mysqldump -h127.0.0.1 -uroot ...
- tomcat 之 session服务器 (memcache)
#: 在tomcat各节点安装memcached [root@node1 ~]# yum install memcached -y #: 下载tomcat所需的jar包(此处在视频中找软件) [roo ...
- ganglia -api
setup 命令: virtualenv ve source ve/bin/activate pip install -r requirements.txt python ganglia/gangli ...
- Spring Cloud使用
一.创建提供者工程01-provider-8081 (1) 创建工程 创建一个Spring Initializr工程,并命名为01-provider-8081.导入Lombok.Web.JPA及MyS ...
- Go语言核心36讲(Go语言实战与应用二十五)--学习笔记
47 | 基于HTTP协议的网络服务 我们在上一篇文章中简单地讨论了网络编程和 socket,并由此提及了 Go 语言标准库中的syscall代码包和net代码包. 我还重点讲述了net.Dial函数 ...
- Jenkins配置管理工具
安装完成后,配置管理工具 安装并配置git yum -y install git 编辑git环境变量为/usr/bin/git 安装并配置maven wget https://mirrors.tuna ...
- 推荐2个Mac OS X上的JSON工具
原文:http://www.giser.net/?p=887 1 visual JSON 能够将JSON串以列表的方式展示,方便对JSON数据的解析. 2 JSONModeler 可以解析JSON串生 ...
- Spring5 概述及Spring IOC学习
Spring Framework 5 1. Spring框架 1.1 Spring框架概述 1.2 主要内容 Spring框架是一个开源的JavaEE的应用程序 主要核心是 IOC(控制反转)和AOP ...