2017-04-14 60 views
0
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美元利润的类。要计算每个班级的收益率,请将房间容量乘以班级价格,然后减去房间的费用。要计算房间的成本乘以持续时间除以60. 乘以costperhour但它没有给出正确的结果,有人可以告诉我,我犯了我的错误。谢谢。表格被附上。MySQl Query给出错误结果

enter image description here

+0

初次腮红你有3个表被加入到子查询中,但只有一个表连接标准。如果你使用内连接语法和''''符号,这将更加明显,这样的疏忽不会被错过。具体来说,你说r和t如何关联'r.roomnum = tt.roomnum',但你没有说明如何但不是r和t或tt和t关联 – xQbert

+0

从YogaTimeTable中选择*; 从YogaTimeTable 删除 凡RoomNum IN(来自YogaRooms R, YogaTypes吨, YogaTimeTable选择tt.RoomNum TT 其中r.RoomNum = tt.roomNum和tt.YogaID = t.YogaID 和((R。 RoomCapacity * t.ClassPrice) - (r.CostPerHour * tt.duration/60))<200); 从YogaTimeTable中选择*; – Obi

+0

我加入其他两个表后结果是一样的,它删除了错误的类...... – Obi

回答

1

对我来说,它看起来像你有两个问题。

  1. t和tt之间的交叉连接存在,应该解决。
  2. 您正试图根据YogaTimeTable的不完整或部分关键字进行删除。 YogaTimeTable的独特密钥出现成为YogaID,StartTime,Day和RoomNum。我这样说是因为同一个瑜伽类型可以在不同的日子同时在同一个房间里,或者在不同的开始时间在同一天的同一个房间里。因此,我认为YogaTimeTable的唯一关键是这四个领域的复合关键。所以在删除时,你需要使用完整的密钥,而不是部分密钥。

所以这会导致。 。

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

+0

我明白你的意思我不知道..但是当我尝试运行查询时,它说Msg 102,Level 15,State 1,Line number 3 'YTT'附近的语法不正确。 – Obi

+0

@strawberry只是从原来的复制/粘贴。你是对的,不需要。 – xQbert

+0

你真棒!!!!! – Obi

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) 

现在删除不需要的rable sessions ...

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) 
+0

我认为最后的结果需要包含一天作为连接的一部分。您可以在同一天的同一时间为同一个房间在同一个星期的不同日期使用相同的类型。 – xQbert

+0

啊......我做了这个假设,成本不会改变......但是我看到现在的小时数等......可以不同。我更新了包含Day的答案。 – RMathis

+0

非常感谢,但我有一个问题,为什么当我尝试运行查询时,它返回一个空行? – Obi