2012-04-13 63 views

回答

2

不,您不能在FROM子句中使用CASE語句。您將需要兩個表中的加入,則基於該值相關的表拉場:

SELECT 
    tale0.*, 
    CASE WHEN tale0.id = 1 THEN class.Column1 ELSE hamburger.Column1 END as Column1, 
    CASE WHEN tale0.id = 1 THEN class.Column2 ELSE hamburger.Column2 END as Column2, 
    ... 
FROM 
    table0, class, hamburger 
-- include join conditions 
+0

謝謝,我會試試看。 – 2012-04-14 17:40:54

0

另一種方式來處理,這是

SELECT TALE0.*, CLASS.*, NULL AS HF1, NULL AS HF2, NULL AS HF3 
    FROM TALE0 
    INNER JOIN CLASS 
    ON CLASS.whatever = TALE0.whatever 
UNION ALL 
SELECT TALE0.*, NULL AS CF1, NULL AS CF2, NULL AS CF3, HAMBURGER.* 
    FROM TALE0 
    INNER JOIN HAMBURGER 
    ON HAMBURGER.whatever = TALE0.whatever 

你需要有儘可能多的HF1,HF2,HF3等字段,因爲您在HAMBURGER中有字段 - 並且使用HAMBURGER中的字段名稱而不是HF1,HF2,HF3等。同樣,CF1,CF2等應由CLASS中的字段名稱替換。

祝你好運。

相關問題