Select * from YogaTimeTable;
Delete
from YogaTimeTable
Where RoomNum IN (select tt.RoomNum
from YogaRooms r,
YogaTypes t,
YogaTimeTable tt
where r.RoomNum = tt.roomNum
and ((r.RoomCapacity * t.ClassPrice) - (r.CostPerHour * tt.duration / 60)) < 200);
Select * from YogaTimeTable;
目标是从时间表中删除任何可以赚取不到200美元利润的类。要计算每个类的盈利能力,请将roomcapacity乘以classprice,然后减去房间的成本。计算房间的成本乘以costperhour乘以持续时间除以60。 但它没有给出正确的结果,有人能告诉我我犯了哪些错误。谢谢。附表。
答案 0 :(得分:1)
对我来说,看起来你有两个问题。
所以这会导致。
DELETE FROM YogaTimeTable
WHERE exists
(SELECT 1
FROM YogaRooms r
INNER JOIN YogaTimeTable tt
on r.RoomNum = tt.roomNum
INNER JOIN YogaTypes t
on tt.YogaID = t.YogaID
WHERE YogaTimeTable.YogaID = TT.YogaID
and YogaTimeTable.RoomNum = TT.RoomNum
and YogaTimeTable.StartTime = TT.StartTime
and YogaTimeTable.Day = TT.Day
and ((r.RoomCapacity * t.ClassPrice) - (r.CostPerHour * tt.duration / 60)) < 200);
根据:我可以使用相关的子查询来删除我只能对表格进行别名.... https://bugs.mysql.com/bug.php?id=2920
答案 1 :(得分:1)
所有班级的盈利能力......
select ytt.YogaID,
ytt.Day,
ytt.StartTime,
ytt.RoomNum,
yt.ClassPrice,
ifnull(ytt.Duration,0) as Duration,
ifnull(yr.CostPerHour,0) as CostPerHour,
ifnull(yr.RoomCapacity,0) as RoomCapacity,
round( ifnull(yr.RoomCapacity,0)*yt.ClassPrice
- (ifnull(yr.CostPerHour,0)*ifnull(ytt.Duration,0)/60)
, 2) as Profitability
from YogaTypes yt
left join YogaTimeTable ytt on (ytt.YogaID=yt.YogaID)
left join YogaRooms yr on (yr.RoomNum=ytt.RoomNum);
+--------+-----------+-----------+---------+------------+----------+-------------+--------------+---------------+
| YogaID | Day | StartTime | RoomNum | ClassPrice | Duration | CostPerHour | RoomCapacity | Profitability |
+--------+-----------+-----------+---------+------------+----------+-------------+--------------+---------------+
| DRU | Wednesday | 10:30:00 | 1 | 18.50 | 60.00 | 100.00 | 20 | 270.00 |
| DRU | Tuesday | 17:00:00 | 2 | 18.50 | 90.00 | 50.00 | 10 | 110.00 |
| SUN | Monday | 07:30:00 | 3 | 18.00 | 60.00 | 150.00 | 25 | 300.00 |
| HAT | Tuesday | 07:30:00 | 4 | 20.00 | 90.00 | 70.00 | 15 | 195.00 |
| HAT | Monday | 18:30:00 | 4 | 20.00 | 60.00 | 70.00 | 15 | 230.00 |
| NULL | NULL | NULL | NULL | 17.00 | 0.00 | 0.00 | 0 | 0.00 |
+--------+-----------+-----------+---------+------------+----------+-------------+--------------+---------------+
6 rows in set (0.00 sec)
盈利能力低于预期的课程......
select ytt.YogaID,
ytt.Day,
ytt.StartTime,
ytt.RoomNum
from YogaTypes yt
left join YogaTimeTable ytt on (ytt.YogaID=yt.YogaID)
left join YogaRooms yr on (yr.RoomNum=ytt.RoomNum)
where ifnull(yr.RoomCapacity,0)*yt.ClassPrice
- (ifnull(yr.CostPerHour,0)*ifnull(ytt.Duration,0)/60) < 200;
+--------+---------+-----------+---------+
| YogaID | Day | StartTime | RoomNum |
+--------+---------+-----------+---------+
| DRU | Tuesday | 17:00:00 | 2 |
| HAT | Tuesday | 07:30:00 | 4 |
| NULL | NULL | NULL | NULL |
+--------+---------+-----------+---------+
3 rows in set (0.00 sec)
现在删除不受欢迎的会话......
delete tt.*
from YogaTimeTable tt,
(select ytt.YogaID,
ytt.Day,
ytt.StartTime,
ytt.RoomNum
from YogaTypes yt
left join YogaTimeTable ytt on (ytt.YogaID=yt.YogaID)
left join YogaRooms yr on (yr.RoomNum=ytt.RoomNum)
where ifnull(yr.RoomCapacity,0)*yt.ClassPrice
- (ifnull(yr.CostPerHour,0)*ifnull(ytt.Duration,0)/60) < 200
) as unprof
where tt.YogaID=unprof.YogaID
and tt.RoomNum=unprof.RoomNum
and tt.Day=unprof.Day
and tt.StartTime=unprof.StartTime;
Query OK, 2 rows affected (0.00 sec)