簡體   English   中英

MySQl查詢給出錯誤結果

[英]MySQl Query giving wrong result

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除以。但是它沒有給出正確的結果,有人可以告訴我我犯錯的地方了。 謝謝。 附表。

在此處輸入圖片說明

在我看來,您有兩個問題。

  1. t和tt之間存在交叉連接,應該解決。
  2. 您正在嘗試根據YogaTimeTable的不完整或部分密鑰進行刪除。 YogaTimeTable的唯一鍵似乎是YogaID,StartTime,Day和RoomNum。 我之所以這樣說,是因為相同的瑜伽類型可以在同一天的同一時間在同一房間,或者在同一天的同一時間在同一房間。 因此,我認為YogaTimeTable的唯一鍵是這4個字段的組合鍵。 因此,刪除時需要使用完整密鑰,而不是部分密鑰。

因此,這將導致

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

所有類別的盈利能力...

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM