2017-04-14 59 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