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之前的架构:

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,但它实际上在那里工作..要么

a)我没有正确地重新创建它 b)也许是 NaviCat 弄乱了结果,尽管我对此表示怀疑

更新 #2:找到原因 - 这是因为我使用的 LIKE(正确地)匹配子字符串,所以 '2/content' 将匹配 '22/content' 等等。

我应该如何以有效的方式更改 cms_collection 上的 INNER JOIN 以进行完全匹配?

4

1 回答 1

1

Try removing the wildcard from the start of the LIKE string-matching join condition, like so:

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.

于 2013-06-13T15:02:35.967 回答