2012-04-27 63 views
1

我正在運行此查詢,該查詢正在填充我然後從中導出的臨時表。價格單是通過加倍票價來計算的。但是,對於某些行來說,該類別無論如何都是單一票價,所以我不想將其加倍。查詢實際上是嵌入在PHP中的字符串,但SQL是這樣的:如果另一個字段等於5或10,則不要乘以一個字段

INSERT INTO temp 
    SELECT cruises.code AS sailingId, live, 'USD', 'optioncode', 'rateOptionName', '2', 
      category AS cabinCategory, fare*2 AS priceSingle, fare AS priceDouble, 
      supplements_usa.adult AS priceAdditional 
     FROM fares_usa, supplements_usa, cruises 
    WHERE fares_usa.cruise_id = supplements_usa.cruise_id 
     AND cruises.id = fares_usa.cruise_id AND cruises.live = 'Y' 

我怎樣才能避免做fare*2category等於5或10?

回答

5
CASE WHEN category = 5 OR category = 10 THEN fare ELSE fare * 2 END 
+0

偉大的,只是代替恐懼* 2 AS價格單 – Rich 2012-04-27 15:03:55

+0

是的; 'CASE ... END'是一個替換你的'票價* 2'表達式的表達式。 – 2012-04-27 15:07:08

+0

是的,這是做到了,會打勾,當它讓我,謝謝@Jonathan Leffler – Rich 2012-04-27 15:07:58

相關問題