2015-02-10 20 views
2

我對PHP IF()和ELSEIF()完全沒問題。但是今天我開始使用MySQL命令,我有一個很長的MySQL查詢,我無法理解我如何修改它。有問題了解MySQL IF()和ELSE()

下面是代碼:

$db->colums[] = " 
IF(
    (SELECT 
     IF(p.delivery = '1',IF(distance < deliverymilesto1, deliverysurcharge1, 
      IF (distance < deliverymilesto2, deliverysurcharge2, 
       IF (distance < deliverymilesto3, deliverysurcharge3, 
        IF (distance < deliverymilesto4, deliverysurcharge4, 
         IF(distance < deliverymilesto5,deliverysurcharge5,0) 
        ) 
       ) 
      ) 
     ), 0) 
     FROM products prod WHERE prod.id_product = p.id_product 
    ) > 0, 
    (p.price + (SELECT 
     IF(p.delivery = '1',IF(distance < deliverymilesto1, deliverysurcharge1, 
      IF (distance < deliverymilesto2, deliverysurcharge2, 
       IF (distance < deliverymilesto3, deliverysurcharge3, 
        IF (distance < deliverymilesto4, deliverysurcharge4, 
         IF(distance < deliverymilesto5,deliverysurcharge5,0) 
         ) 
        ) 
       ) 
      ), 0) 
      FROM products prod WHERE prod.id_product = p.id_product 
     )), 
    (p.price) 
)as pricedelivery"; 

我需要做的就是創建一個ELSE語句,如果p.delivery等於 '2',並添加以下代碼:

IF(p.delivery = '2', IF(find_in_set('".$outcoder."', deliveryoutcode1), deliveryoutcharge1, 
IF(find_in_set('".$outcoder."', deliveryoutcode2), deliveryoutcharge1, deliveryoutcharge2, 0)), 0) 

在PHP這很簡單,因爲它會是:

if($delivery = '1') { 
...code here 
} elseif($delivery = '2') { 
...otherwise 
} 

但我只是不明白如何改變我的陳述的邏輯。也許我的大腦已經疲憊了,因爲我今天一直在學習正常化。任何人都可以幫助我,謝謝。

編輯:我已經花了整個上午清理MySQL,其結果是上面的,基於我迄今爲止瞭解的MySQL。

但是,我現在想根據我收到的有用評論,使用case來重寫這個。我在我的頁面上有5個這樣的MySQL查詢,我想知道是否有人可以重寫case格式的上述內容,然後我可以用它來重寫其他查詢?謝謝。

+0

首先要認識到:sql不是一種編程語言。所以最好的做法是甚至不嘗試將命令式編程習慣用於它。 – zerkms 2015-02-10 00:48:38

+2

將語句轉換爲'case'。 。 。它會更有意義。 – 2015-02-10 01:00:56

+0

好吧,我將不得不規範我的數據庫,如果我想這個工作。我把東西作爲一個數組存儲在一個表格單元中是愚蠢的。由於這份工作對我來說太過艱難,我已經去oDesk聘請一個人,我知道我的侷限性。但是,感謝所有人關心/ Luke – 2015-02-10 14:17:29

回答

2
SELECT 
    price + 
    case delivery 
     when '1' then 
      case 
       when distance < deliverymilesto1 then deliverysurcharge1 
       when distance < deliverymilesto2 then deliverysurcharge2 
       when distance < deliverymilesto3 then deliverysurcharge3 
       when distance < deliverymilesto4 then deliverysurcharge4 
       when distance < deliverymilesto5 then deliverysurcharge5 
       else 0 
      end 
     when '2' then 
      case 
       when distance < deliverymilesto1 then deliveryoutcharge1 
       when distance < deliverymilesto2 then deliveryoutcharge2 
       when distance < deliverymilesto3 then deliveryoutcharge3 
       when distance < deliverymilesto4 then deliveryoutcharge4 
       when distance < deliverymilesto5 then deliveryoutcharge5 
       else 0 
      end 
     else 0 
    end 
FROM products 

IF()作爲一個函數是不一樣的,你已經習慣的分支。第一個參數是一個條件,第二個參數是當條件評估爲true時返回的結果,第三個參數是false時的相反情況。

上面的代碼只是在第三個參數(else分支)中使用了很多嵌套來完成通常使用IF ... ELSE ...或與CASE表達式等效的操作。

您必須將其合併到您的查詢的其餘部分,因爲您已將其中的部分內容遺留下來。

+0

仍然試圖將其整合到我的代碼中,我會繼續嘗試,但感謝迄今爲止的輸入。我明天會告訴你我的進展。 – 2015-02-10 01:40:06

+0

好,所以我的問題更新@ shawnt00,但沒有運氣的代碼,你能看到我可能做錯了什麼?我首先在原始代碼上測試case語句。謝謝。 – 2015-02-10 02:14:15

+0

你根本不需要任何'IF()'。但我不確定整個「colums」是關於什麼的。它是作爲更大查詢的一部分構建的列的列表嗎? – shawnt00 2015-02-10 04:23:55