根据菜单中的类型提取新闻/文章/子页面参数

发布于 2024-12-05 11:26:22 字数 692 浏览 1 评论 0原文

我编写了一个简单的 sql 查询 - http://pastebin.com/AdJAabzb - 但它根本不是最佳的。 我想在当 menu.type == news 时对 'news' 进行外连接,或者当 menu.type == 'article' 时对 'articles' 进行外连接,或者当 menu.type == 'page' 时对 'subpages' 进行外连接。如果类型不同(例如 none 或 ext),seo_filename 应为 NULL。

SELECT 
 menu.*
 , news.seo_filename AS news_seo_filename
 , articles.seo_filename AS article_seo_filename
 , subpages.seo_filename AS subpage_seo_filename
FROM menu
LEFT OUTER JOIN news ON menu.link = news.id
LEFT OUTER JOIN articles ON menu.link = articles.id
LEFT OUTER JOIN subpages ON menu.link = subpages.id 
ORDER BY lft

如何使用带有 if'f 或 sth 的一个查询来完成此操作?

I wrote a simple sql query - http://pastebin.com/AdJAabzb - but its not optimal at all.
I want to do outer join on 'news' when menu.type == news OR on 'articles' when menu.type == 'article' OR on 'subpages' when menu.type == 'page'. If type is different (eg. none or ext) seo_filename should be NULL.

SELECT 
 menu.*
 , news.seo_filename AS news_seo_filename
 , articles.seo_filename AS article_seo_filename
 , subpages.seo_filename AS subpage_seo_filename
FROM menu
LEFT OUTER JOIN news ON menu.link = news.id
LEFT OUTER JOIN articles ON menu.link = articles.id
LEFT OUTER JOIN subpages ON menu.link = subpages.id 
ORDER BY lft

How to do it using one query with if'f or sth?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-12-12 11:26:22

我的“不聪明的联盟”解决方案。这在 Postgres 中是合理的,因为键字段可以从子查询中提升出来。

注意:对 UNION 的需求几乎总是数据模型中设计缺陷的结果。

WITH  un AS (
  SELECT 'articles' AS menutype
  , ar.id AS id
  , ar.seo_filename AS filename
  FROM articles ar
  UNION
  SELECT 'news' AS menutype
  , ne.id AS id
  , ne.seo_filename AS filename
  FROM news ne
  UNION
  SELECT 'pages' AS menutype
  , pa.id AS id
  , pa.seo_filename AS filename
  FROM pages pa
  ) 
SELECT
 me.*
 , un.filename
 FROM menu me
  , un
 WHERE me.menutype = un.menutype
 AND me.id = un.id
  ;

编辑:
注意:我稍微更改了列名......

My "unsmart union" solution. This will perform reasonable in Postgres, because the keyfields can be hoisted out of the subqueries.

NOTE: The need for an UNION is nearly always a result of a design flaw in the data model.

WITH  un AS (
  SELECT 'articles' AS menutype
  , ar.id AS id
  , ar.seo_filename AS filename
  FROM articles ar
  UNION
  SELECT 'news' AS menutype
  , ne.id AS id
  , ne.seo_filename AS filename
  FROM news ne
  UNION
  SELECT 'pages' AS menutype
  , pa.id AS id
  , pa.seo_filename AS filename
  FROM pages pa
  ) 
SELECT
 me.*
 , un.filename
 FROM menu me
  , un
 WHERE me.menutype = un.menutype
 AND me.id = un.id
  ;

EDIT:
NOTE: I changed the columnnames a bit ...

好倦 2024-12-12 11:26:22

尝试使用 CASE:

SELECT 
    M.*,
    CASE(M.type)
        WHEN 'news' THEN N.seo_filename
        WHEN 'articles' THEN A.seo_filename
        WHEN 'page' THEN S.seo_filename
        ELSE NULL
    END AS seo_filename
FROM 
    menu M
        LEFT OUTER JOIN news N ON M.link = N.id
        LEFT OUTER JOIN articles A ON M.link = A.id
        LEFT OUTER JOIN subpages S ON M.link = S.id 
ORDER BY 
    M.lft 

我不确定这个 lft 字段来自哪里,但我假设来自表格菜单。

Try with CASE:

SELECT 
    M.*,
    CASE(M.type)
        WHEN 'news' THEN N.seo_filename
        WHEN 'articles' THEN A.seo_filename
        WHEN 'page' THEN S.seo_filename
        ELSE NULL
    END AS seo_filename
FROM 
    menu M
        LEFT OUTER JOIN news N ON M.link = N.id
        LEFT OUTER JOIN articles A ON M.link = A.id
        LEFT OUTER JOIN subpages S ON M.link = S.id 
ORDER BY 
    M.lft 

I am not sure where this lft field comes from, but I assume from table menu.

迷爱 2024-12-12 11:26:22

另一种可能的解决方案:

SELECT 
 menu.*
 , COALESCE( news.seo_filename, articles.seo_filename, subpages.seo_filename)
   AS seo_filename
FROM menu
LEFT OUTER JOIN news ON menu.link = news.id
LEFT OUTER JOIN articles ON menu.link = articles.id
LEFT OUTER JOIN subpages ON menu.link = subpages.id 
ORDER BY lft
;

Another possible solution:

SELECT 
 menu.*
 , COALESCE( news.seo_filename, articles.seo_filename, subpages.seo_filename)
   AS seo_filename
FROM menu
LEFT OUTER JOIN news ON menu.link = news.id
LEFT OUTER JOIN articles ON menu.link = articles.id
LEFT OUTER JOIN subpages ON menu.link = subpages.id 
ORDER BY lft
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文