根据菜单中的类型提取新闻/文章/子页面参数
我编写了一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的“不聪明的联盟”解决方案。这在 Postgres 中是合理的,因为键字段可以从子查询中提升出来。
注意:对 UNION 的需求几乎总是数据模型中设计缺陷的结果。
编辑:
注意:我稍微更改了列名......
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.
EDIT:
NOTE: I changed the columnnames a bit ...
尝试使用 CASE:
我不确定这个
lft
字段来自哪里,但我假设来自表格菜单。Try with CASE:
I am not sure where this
lft
field comes from, but I assume from table menu.另一种可能的解决方案:
Another possible solution: