2013-06-13 44 views
2
SELECT 

    GROUP_CONCAT(if(meta_summary = 'content', content, NULL)) AS content, 
    GROUP_CONCAT(if(meta_summary = 'supplemental', content, NULL)) AS supplemental, 
    GROUP_CONCAT(if(meta_summary = 'heading', content, NULL)) AS heading, 

cms_pages.meta_filename as filename 

FROM 
cms_pages 

INNER JOIN cms_collection 

ON 

cms_collection.collection_id LIKE CONCAT('%', cms_pages.page_id, '/heading%') 
OR cms_collection.collection_id LIKE CONCAT('%', cms_pages.page_id, '/content%') 
OR cms_collection.collection_id LIKE CONCAT('%', cms_pages.page_id, '/supplemental%') 

INNER JOIN 

cms_content ON cms_collection.collection_id = cms_content.collection_id 

WHERE 
site_id = 51 

GROUP BY 
cms_pages.page_id 

架構在GROUP BY:JOINing後旋轉表錯誤?

Filename  Content 

pageA   (Heading Content) 
pageA   (Content) 
pageA   (Supplemental Content) 

所需的規則集:

Filename  Heading   Content   Supplemental 
pageA   (Heading Content) (Content)   (Supp. Content) 

我試着讀http://www.artfulsoftware.com/infotree/queries.php#78但想不通這是爲什麼不爲我工作 - 它似乎是不正確的組連接行..有些是正確的,有些看起來像他們不正確地合併來自另一頁的內容。這可能是因爲我需要使用組concat做一個子查詢,因爲我的所有連接都是?

更新#1:創建一個sqlfiddle @http://sqlfiddle.com/#!2/fe3e3/1,但它的實際工作有..要麼

一)我沒有重新建立正確 B)也許是Navicat的多數民衆贊成搞亂了的結果,雖然我懷疑它

更新#2:找到原因 - 這是因爲我使用的是(正確)匹配子字符串,所以'2 /內容'將匹配'22 /內容'等等。

我應該如何在cms_collection上以高效方式更改我的INNER JOIN以完成精確匹配?

+0

此刻,我不禁要將「內容」的內容改爲HTML,標題是直接文字,補充HTML也是如此,如果有任何意義的話,請儘量嘗試明天舉個例子。 –

+0

好的。我想清楚爲什麼會發生這種情況 - 因爲我使用的LIKE(正確)匹配子字符串,所以'2/content'匹配'22/content'等等。我應該如何以有效的方式改變我在cms_collection上的INNER JOIN來完成完全匹配? –

回答

1

嘗試從LIKE字符串匹配的開始去除通配符連接條件,就像這樣:

SELECT 
    GROUP_CONCAT(if(meta_summary = 'content', content, NULL)) AS content, 
    GROUP_CONCAT(if(meta_summary = 'supplemental', content, NULL)) AS supplemental, 
    GROUP_CONCAT(if(meta_summary = 'heading', content, NULL)) AS heading, 
    cms_pages.meta_filename as filename 
FROM cms_pages 
INNER JOIN cms_collection ON 
    cms_collection.collection_id LIKE CONCAT(cms_pages.page_id, '/heading%') 
    OR cms_collection.collection_id LIKE CONCAT(cms_pages.page_id, '/content%') 
    OR cms_collection.collection_id LIKE CONCAT(cms_pages.page_id, '/supplemental%') 
INNER JOIN 
    cms_content ON cms_collection.collection_id = cms_content.collection_id 
WHERE 
    site_id = 53 
GROUP BY 
    cms_pages.page_id 

SQLFiddle here