帮助优化子查询

发布于 2024-10-01 16:37:46 字数 492 浏览 3 评论 0原文

此查询获取与其他新闻文章类似标记的新闻文章。我是否可以消除这些我没有看到的子查询之一?

SELECT id, 
       title, 
       comment_count
  FROM NEWS
 WHERE (franchise_id = 1 OR franchise_id = 0)
   AND draft = 0
   AND id != 459
   AND id IN (SELECT news_id
                FROM news_tag_association
               WHERE tag_id IN (SELECT tag_id
                                  FROM news_tag_association
                                 WHERE news_id = 459))                               
 LIMIT 0, 5;

This query fetches news articles that are tagged similarly to other news articles. Is it possible I can eliminate one of these subqueries that I'm not seeing?

SELECT id, 
       title, 
       comment_count
  FROM NEWS
 WHERE (franchise_id = 1 OR franchise_id = 0)
   AND draft = 0
   AND id != 459
   AND id IN (SELECT news_id
                FROM news_tag_association
               WHERE tag_id IN (SELECT tag_id
                                  FROM news_tag_association
                                 WHERE news_id = 459))                               
 LIMIT 0, 5;

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

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

发布评论

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

评论(1

别挽留 2024-10-08 16:37:46

是的,没有 GROUP BY 或类似的,所以你可以使用普通的自连接:

SELECT
    id, title, comment_count
FROM news
JOIN news_tag_association n2t1 ON (news.id = n2t1.news_id)
JOIN news_tag_association n2t2 ON (n2t1.tag_id = n2t2.tag_id)
WHERE franchise_id IN (0, 1)
    AND draft = 0
    AND id != 459
    AND n2t2.news_id = 459
LIMIT 0,5;

Yes, there is no GROUP BY or similar, so you can use normal self-joins:

SELECT
    id, title, comment_count
FROM news
JOIN news_tag_association n2t1 ON (news.id = n2t1.news_id)
JOIN news_tag_association n2t2 ON (n2t1.tag_id = n2t2.tag_id)
WHERE franchise_id IN (0, 1)
    AND draft = 0
    AND id != 459
    AND n2t2.news_id = 459
LIMIT 0,5;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文