2013-08-22 65 views
0

我堆放在以下查詢上。我需要創建一個if語句或類似的東西來區分哪些「CASE」應該添加到我的選擇查詢中。如果聲明區分哪個「Case」查詢應該添加到選擇查詢

If page_type = 'Region' or 'City'這添加到查詢:

(CASE When landing_page like concat('%',path_id,'%') and 
(LENGTH(landing_page) - LENGTH(REPLACE(landing_page, '/' ,''))/LENGTH('/')) = 2 
Then 'True' Else 'False' End as 'correct lp' ,) 

Elseif page_type = 'Item'這添加到查詢:

(CASE When landing_page like concat('%',path_id,'%') and 
(LENGTH(landing_page) - LENGTH(REPLACE(landing_page, '/' ,''))/LENGTH('/')) = 3 
Then 'True' Else 'False' End as 'correct lp' ,) 

Elseif page_type = 'POI (Alle)'這添加到查詢:

(CASE When landing_page like concat('%',path_id,'%') and 
(LENGTH(landing_page) - LENGTH(REPLACE(landing_page, '/' ,''))/LENGTH('/')) = 3 and 
'Hotel' NOT IN landing_page 
Then 'True' Else 'False' End as 'correct lp' ,) 

個案例查詢應根據if結果被添加到下面的查詢:

SELECT 
    MONTHNAME(k.crawl_date) As 'Month', 
    YEAR(k.crawl_date) As 'Year', 
    WEEKOFYEAR(k.crawl_date) As 'Week', 
    k.landing_page, 
    k.pos, 
    CASE WHEN k.pos = 0 THEN 'No Rank' WHEN k.pos < 4 THEN 'Top 3' WHEN k.pos < 11 THEN 'Page 1' 
     WHEN k.pos < 21 THEN 'Page 2' WHEN k.pos < 31 THEN 'Page 3' WHEN k.pos < 41 THEN 'Page 4' 
     WHEN k.pos < 51 THEN 'Page 5' WHEN k.pos < 61 THEN 'Page 6' WHEN k.pos < 71 THEN 'Page 7' 
     WHEN k.pos < 81 THEN 'Page 8' WHEN k.pos < 91 THEN 'Page 9' WHEN k.pos < 101 THEN 'Page 10' 
     WHEN k.pos < 111 THEN 'Page 11' END as 'Page', 
k.path_id, 
k.domain, 
x.keyword, 
x.engine_id, 
l.bucket, 
l.page_type, 
l.item_label 
FROM 
xovi_stats k 
LEFT JOIN xovi_keywords x ON k.keyword_id = x.id 
LEFT JOIN xovi_labels l ON l.id = x.label_id 
+0

「CASE」子句將被添加到「SELECT」部分中,我想呢? – RandomSeed

+0

是的,這是正確的。 – CompFreak

回答

1

可以嵌套CASE條款是這樣的:

SELECT 
    CASE 
     WHEN (page_type IN ('Region', 'City')) THEN 
      CASE 
       WHEN landing_page LIKE ... THEN 'True' 
       ELSE 'False' 
      END 
     WHEN (page_type = 'Item') THEN 
      CASE 
       ... 
      END 
     WHEN (page_type = 'POI (Alle)') THEN 
      CASE 
       ... 
      END 
    END AS lp, 
    MONTHNAME(k.crawl_date) As 'Month', -- other fields 
    ... 

或者你還可以嵌套IF(),你的建議:

SELECT 
    IF ((page_type IN ('Region', 'City')), 
     -- if (page_type IN ('Region', 'City')) then 
     CASE 
      WHEN landing_page LIKE ... THEN 'True' 
      ELSE 'False' 
     END, 
     -- else 
     IF ((page_type = 'Item'), 
      -- if (page_type = 'Item') then 
      CASE 
       ... 
      END, 
      -- else 
      IF (page_type = 'POI (Alle)', 
       -- if (page_type = 'POI (Alle)') then 
       CASE 
        ... 
       END, 
       -- else 
       NULL 
      ) 
     ) 
    ) AS lp, 
    MONTHNAME(k.crawl_date) As 'Month', -- other fields 
    ... 

題外話說明:

  • 你可以用簡單的IF()取代你的兩個分支CASE聲明:
SELECT CASE 
    WHEN landing_page LIKE ... THEN 'True' 
    ELSE 'False' 
END AS alias ; 

-- strictly equivalent to 

SELECT IF(
    landing_page LIKE ..., 
    'True', 
    'False' 
) AS alias ; 
  • 可以替代你現有大規模的CASE有:
CASE 
    WHEN k.pos = 0 THEN 'No Rank' 
    WHEN k.pos < 4 THEN 'Top 3' 
    ELSE CONCAT('Page ', (k.pos -1) DIV 10 +1) 
END AS 'Page' 
+0

謝謝,查詢完美 – CompFreak