Verified Commit aeca3c8e authored by Kiryuu Sakuya's avatar Kiryuu Sakuya 🎵
Browse files

Half way No.1

parent 506010eb
......@@ -62,7 +62,7 @@ SELECT P.Pno AS "包裹物流号", C.Cname AS "快递员姓名", D.Dtime AS "配
### 2. 查询2017-4-1至2017-5-1(包括2017-4-1和2017-5-1这两天)配送包裹数量至少为100个的快递员的姓名、年龄和底薪
>
> `GROUP BY` 是聚合函数,可以把值相同的都放到一起。
```mysql
SELECT C.Cname, C.Cage, C.Csalary
......@@ -92,10 +92,45 @@ WHERE Cage IN (SELECT Cage FROM C WHERE Cage < 40);
### 4. 给定仓库的名称,输出为在该仓库有存储记录,且包裹状态为“派件中”的配送信息,输出格式为“快递员姓名/收货地址/收件人姓名/收件人联系方式/配送状态”,这里的“配送状态”设置方法为:若配送时间(Dtime)距今超过5天,则设置为“异常”;否则设置为“正常”
```mysql
-- SELECT C.Cname AS "快递员姓名", P.Pto AS "收货地址", P.Preceiver AS "收件人姓名", P.Ptel AS "收件人联系方式", P.Pstatus AS "配送状态" FROM C, P;
DELIMITER //
CREATE PROCEDURE Deliver_Status()
BEGIN
DECLARE Courier VARCHAR(10);
DECLARE Address VARCHAR(100);
DECLARE Receiver VARCHAR(10);
DECLARE Tel VARCHAR(20);
DECLARE Deliver_Time DATE;
DECLARE Deliver_Status VARCHAR(10); -- 设置配送状态
DECLARE COUNT INT DEFAULT 0;
DECLARE I INT DEFAULT 0;
DECLARE Cursor_Deliver CURSOR FOR SELECT C.Cname, P.Pto, P.Preceiver, P.Ptel, D.Dtime FROM C, P, D;
SELECT COUNT(*) INTO COUNT FROM P;
SET @Success = "正常";
SET @UnSuccess = "异常";
OPEN Cursor_Deliver;
REPEAT
SET I := I + 1;
FETCH Cursor_Deliver INTO Courier, Address, Receiver, Tel, Deliver_Time;
IF DATEDIFF(CURRENT_DATE(), @Deliver_Time) >= 5 THEN
SET Deliver_Status = @Success;
ELSE SET Deliver_Status = @UnSuccess;
END IF;
SELECT Courier AS "快递员姓名", Address AS "收货地址", Receiver AS "收件人姓名", Tel AS "收件人联系方式", Deliver_Status AS "配送状态";
UNTIL I >= COUNT END REPEAT;
CLOSE Cursor_Deliver;
END;
//
DELIMITER ;
CALL Deliver_Status();
```
### 5. 输出2017-4-1至2017-5-1期间(不包括2017-4-1和2017-5-1这两天),收货地址包含“景德镇”的配送信息,输出格式化为“快递员姓名/物流号”,多个物流号之间以逗号隔开
```mysql
```
### 6. 用SQL写一个完整性规则,当插入配送信息时,将包裹状态置为“派件中”
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment