2014-09-18 109 views
0

我有下面的查詢,它是由一個PHP函數生成的,它變得非常複雜,但它迄今爲止工作正常。複雜的字段加入MySQL查詢

我想要做的就是由第二場覆蓋一個場如果第三場等於1

所以我想表2 .cpda_meta_val實際上等於表3 .cpda_meta_val當表4 .cpda_meta_val等於1,這樣我就可以在WHERE/ORDER BY子句中使用結果值table2.cpda_meta_val。

我希望我的理解到目前爲止。

這裏是原始查詢:

SELECT 
    mainTable.cpda_id 
FROM 
    cp_data AS mainTable 
LEFT OUTER JOIN 
    cp_data AS table0 
     ON table0.cpda_meta_id = mainTable.cpda_id 
     AND table0.cpda_meta_key = '47248' 
LEFT OUTER JOIN 
    cp_data AS table1 
     ON table1.cpda_meta_id = mainTable.cpda_id 
     AND table1.cpda_meta_key = '47241' 
LEFT OUTER JOIN 
    cp_data AS table2 
     ON table2.cpda_meta_id = mainTable.cpda_id 
     AND table2.cpda_meta_key = '47242' 
LEFT OUTER JOIN 
    cp_data AS table3 
     ON table3.cpda_meta_id = mainTable.cpda_id 
     AND table3.cpda_meta_key = '66838' 
LEFT OUTER JOIN 
    cp_data AS table4 
     ON table4.cpda_meta_id = mainTable.cpda_id 
     AND table4.cpda_meta_key = '66843' 
LEFT OUTER JOIN 
    cp_data AS table5 
     ON table5.cpda_meta_id = mainTable.cpda_id 
     AND table5.cpda_meta_key = '47252' 
LEFT OUTER JOIN 
    cp_data AS table6 
     ON table6.cpda_meta_id = mainTable.cpda_id 
     AND table6.cpda_meta_key = '47255' 
WHERE 
    mainTable.cpda_meta_id = 47236 
    AND mainTable.cpda_active = 1 
    AND mainTable.cpda_meta_key = 'set_element_stub' 
    AND table0.cpda_meta_val = 49297 
    AND table1.cpda_meta_val != 66116 
    AND (
     table5.cpda_meta_val = 497 
     OR table6.cpda_meta_val = 497 
    ) 
ORDER BY 
    table2.cpda_meta_val, 
    mainTable.cpda_seq, 
    mainTable.cpda_id 

這是我在實現上述,不工作,嘗試檢索沒有行:

SELECT 
    mainTable.cpda_id 
FROM 
    cp_data AS mainTable 
LEFT OUTER JOIN 
    cp_data AS table0 
     ON table0.cpda_meta_id = mainTable.cpda_id 
     AND table0.cpda_meta_key = '47248' 
LEFT OUTER JOIN 
    cp_data AS table1 
     ON table1.cpda_meta_id = mainTable.cpda_id 
     AND table1.cpda_meta_key = '47241' 
LEFT OUTER JOIN 
    cp_data AS table2 
     ON table2.cpda_meta_id = mainTable.cpda_id 
     AND table2.cpda_meta_key = '47242' 
LEFT OUTER JOIN 
    cp_data AS table3 
     ON table3.cpda_meta_id = mainTable.cpda_id 
     AND table3.cpda_meta_key = '66838' 
LEFT OUTER JOIN 
    cp_data AS table4 
     ON table4.cpda_meta_id = mainTable.cpda_id 
     AND table4.cpda_meta_key = '66843' 
LEFT OUTER JOIN 
    cp_data AS table5 
     ON table5.cpda_meta_id = mainTable.cpda_id 
     AND table5.cpda_meta_key = '47252' 
LEFT OUTER JOIN 
    cp_data AS table6 
     ON table6.cpda_meta_id = mainTable.cpda_id 
     AND table6.cpda_meta_key = '47255' 
CASE 
    WHEN table4.cpda_meta_val = 1 THEN table3.cpda_meta_val 
    ELSE table2.cpda_meta_val 
END as table2.cpda_meta_val 
WHERE 
    mainTable.cpda_meta_id = 47236 
    AND mainTable.cpda_active = 1 
    AND mainTable.cpda_meta_key = 'set_element_stub' 
    AND table0.cpda_meta_val = 49297 
    AND table1.cpda_meta_val != 66116 
    AND (
     table5.cpda_meta_val = 497 
     OR table6.cpda_meta_val = 497 
    ) 
ORDER BY 
    table2.cpda_meta_val, 
    mainTable.cpda_seq, 
    mainTable.cpda_id 

如果有人可以點我正確的方向,這將是偉大的!謝謝!

編輯 - 對於那些搜索,我用下面戈登的回答,使下面的查詢,現在的工作:

SELECT 
    mainTable.cpda_id, 
    (CASE 
     WHEN table4.cpda_meta_val = 1 THEN table3.cpda_meta_val 
     ELSE table2.cpda_meta_val 
    END) as override_val_0 
FROM 
    cp_data AS mainTable 
LEFT OUTER JOIN 
    cp_data AS table0 
     ON table0.cpda_meta_id = mainTable.cpda_id 
     AND table0.cpda_meta_key = '47248' 
LEFT OUTER JOIN 
    cp_data AS table1 
     ON table1.cpda_meta_id = mainTable.cpda_id 
     AND table1.cpda_meta_key = '47241' 
LEFT OUTER JOIN 
    cp_data AS table2 
     ON table2.cpda_meta_id = mainTable.cpda_id 
     AND table2.cpda_meta_key = '47242' 
LEFT OUTER JOIN 
    cp_data AS table3 
     ON table3.cpda_meta_id = mainTable.cpda_id 
     AND table3.cpda_meta_key = '66838' 
LEFT OUTER JOIN 
    cp_data AS table4 
     ON table4.cpda_meta_id = mainTable.cpda_id 
     AND table4.cpda_meta_key = '66843' 
LEFT OUTER JOIN 
    cp_data AS table5 
     ON table5.cpda_meta_id = mainTable.cpda_id 
     AND table5.cpda_meta_key = '47252' 
LEFT OUTER JOIN 
    cp_data AS table6 
     ON table6.cpda_meta_id = mainTable.cpda_id 
     AND table6.cpda_meta_key = '47255' 
WHERE 
    mainTable.cpda_meta_id = 47236 
    AND mainTable.cpda_active = 1 
    AND mainTable.cpda_meta_key = 'set_element_stub' 
    AND table0.cpda_meta_val = 49297 
    AND table1.cpda_meta_val != 66116 
    AND (
     table5.cpda_meta_val = 497 
     OR table6.cpda_meta_val = 497 
    ) 
ORDER BY 
    override_val_0, 
    mainTable.cpda_seq, 
    mainTable.cpda_id 
+0

我認爲第一件事 - 就是將複雜和難以理解的視爲可理解的名稱和含義,可以應用一些覆蓋。然後重寫查詢,只使用必填字段並對其進行最小更改。 – 2014-09-18 15:48:21

+0

我認爲VIEWs對於這種看法來說是過分的,因爲查詢是通過代碼生成的並且基本上是自動的。我現在使用下面的答案作爲發電機弓的另一個可以說。 – bbeckford 2014-09-19 09:18:14

回答

1

case語句select子句中屬於:

SELECT mainTable.cpda_id, 
     (CASE WHEN table4.cpda_meta_val = 1 THEN table3.cpda_meta_val 
      ELSE table2.cpda_meta_val 
     END) as cpda_meta_val 

你可以把這個表達式直接放在order by中(這似乎是你使用的唯一地方table2_cpda_meta_val

ORDER BY (CASE WHEN table4.cpda_meta_val = 1 THEN table3.cpda_meta_val 
       ELSE table2.cpda_meta_val 
      END), 
     mainTable.cpda_seq, 
     mainTable.cpda_id 
+0

啊當然!謝謝戈登星期五XD – bbeckford 2014-09-19 09:16:55