MySQl查询给出了错误的结果

时间:2017-04-14 16:03:35

标签: mysql sql

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。 但它没有给出正确的结果,有人能告诉我我犯了哪些错误。谢谢。附表。

enter image description here

2 个答案:

答案 0 :(得分:1)

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

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

    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)