1

对于 CMS 系统,我正在尝试以一种优雅的方式在文章关系中搜索用户提供的 url。

我有两个表:一个包含文章 ( cms_article),另一个包含文章的语言特定内容 ( cms_content)。所以 1 行cms_article可以有多个元组,cms_content因为它代表文章的不同语言表示。

不同的文章也可以按reference列嵌套,指的是cms_article如果是顶级文章中的id列 this cms_article.ref= NULL

表格:

cms_article  id  | ref | published  
cms_content  cid | aid | lang      | url | browserTitle | content | etc etc

从这两个表中,我想创建一个可用于查找 URL 的视图。所以我想为每个给定的元组构造一个 totalURL 字段,cms_article每个父/引用作为 URL 的一部分。

例如,让我们回顾一下页面/文章的这种结构:

AID  English                             Dutch                               More languages
1    /Products                           /Producten
4    /Products/Product-2                 /Producten/Product-2
7    /Products/Product-2/screenshots     /Producten/Product-2/afbeeldingen
34   /Products/Product-2/pricing         /Producten/Product-2/prijzen
12   /Products/Product-5                 /Producten/Product-5
6    /About-us                           /Over-ons

每行代表cms_article关系中的一篇文章和 1 个元组。URL 字段来自相应的cms_content.url字段。

如何将每个元组的 totalURL 聚合为视图,以便生成如下视图:

cms_view

aid      = article ID       = integer
lang     = language         = NL|EN|... 
totalURL = agregated URL    = /products/product-2/pricing

对于上面的示例,它必须生成下表:

1  | EN | /Products
1  | NL | /Producten
4  | EN | /Products/Product-2
4  | NL | /Producten/Product-2/afbeeldingen
etc etc 

当然,我可以遍历每个元组cms_content并在 PHP 中找到引用(并对每个级别和每个元组重复此操作)。但是因为这给服务器带来了很大的压力,所以我正在寻找一个优雅的 sql 解决方案,比如视图。

编辑

这是我走了多远,我觉得我很接近。寻求解决方案:结合urlparentURL到一个领域。以及如何在三个层次上做到这一点。

SELECT 
a.id AS aid, c.lang, a.ref,
c.url,
(
    SELECT c2.url
    FROM cms_content AS c2
    WHERE c2.aid = a.ref
    AND
    c2.lang = c.lang
) AS parentURL
FROM cms_content AS c
LEFT JOIN cms_article AS a ON c.aid = a.id
WHERE a.domain = 'some.url'
AND a.published =1
ORDER BY aid
4

1 回答 1

0

如果您了解所有语言,则可以连接子查询。

例子:

SELECT
aid
, CONCATENATE( 
  CASE WHEN (SELECT langcode FROM cms_article x WHERE a.aid = x.aid AND language = 'EN')
  THEN 
    CONCATENATE(
    (SELECT langcode FROM cms_article x WHERE a.aid = x.aid AND language = 'EN')
    , '|')
  ELSE ''
  END,
  CASE WHEN (SELECT langcode FROM cms_article x WHERE a.aid = x.aid AND language = 'DU')
  THEN 
    CONCATENATE(
    (SELECT langcode FROM cms_article x WHERE a.aid = x.aid AND language = 'DU')
    , '|')
  ELSE ''
  END) as language
, CONCATENATE( 
  CASE WHEN (SELECT totalUrl FROM cms_article x WHERE a.aid = x.aid AND language = 'EN')
  THEN 
    CONCATENATE(
    (SELECT totalUrl FROM cms_article x WHERE a.aid = x.aid AND language = 'EN')
    , '|')
  ELSE ''
  END,
  CASE WHEN (SELECT totalUrl FROM cms_article x WHERE a.aid = x.aid AND language = 'DU')
  THEN 
    CONCATENATE(
    (SELECT totalUrl FROM cms_article x WHERE a.aid = x.aid AND language = 'DU')
    , '|')
  ELSE ''
  END) as agregatedUrl
FROM cms_content a
于 2013-06-11T15:59:11.903 回答