如何将这两个复杂的查询合并为一个没有联合的查询?

发布于 2024-12-11 12:53:31 字数 701 浏览 1 评论 0原文

(SELECT posts.id FROM posts 
INNER JOIN discussions ON discussions.post_id = posts.id 
INNER JOIN companies ON discussions.company_id = companies.id 
INNER JOIN subscriptions ON subscriptions.subscribable_id = companies.id AND subscriptions.subscribable_type = 'Company' 
INNER JOIN users ON subscriptions.user_id = users.id WHERE users.id = 6)

UNION 

(SELECT posts.id FROM posts 
INNER JOIN users users_2 ON posts.analyst_id = users_2.id 
INNER JOIN subscriptions ON subscriptions.subscribable_id = users_2.id AND subscriptions.subscribable_type = 'User'
INNER JOIN users ON subscriptions.user_id = users.id WHERE users.id = 6)

很明显,两个查询中的最后一个连接是相同的。只是不确定如何“或”在一起连接。

(SELECT posts.id FROM posts 
INNER JOIN discussions ON discussions.post_id = posts.id 
INNER JOIN companies ON discussions.company_id = companies.id 
INNER JOIN subscriptions ON subscriptions.subscribable_id = companies.id AND subscriptions.subscribable_type = 'Company' 
INNER JOIN users ON subscriptions.user_id = users.id WHERE users.id = 6)

UNION 

(SELECT posts.id FROM posts 
INNER JOIN users users_2 ON posts.analyst_id = users_2.id 
INNER JOIN subscriptions ON subscriptions.subscribable_id = users_2.id AND subscriptions.subscribable_type = 'User'
INNER JOIN users ON subscriptions.user_id = users.id WHERE users.id = 6)

It should be obvious that the last join is the same in both queries.. Just not sure how to "or" together joins.

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

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

发布评论

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

评论(3

金兰素衣 2024-12-18 12:53:31

有些连接似乎是多余的:

  • discussions 可以直接在 company_id 列上连接到 subscriptions

  • posts 可以直接在 analyst_id 列上加入subscriptions

  • 在任一 SELECT 中最后一次加入 users 是不必要的,因为没有从该表中检索到任何数据,并且可以重新应用过滤器 (users.id = 6)到 subscriptions.user_id

所以,我可能会像这样重写查询:

SELECT p.id
FROM posts p
  INNER JOIN discussions d ON d.post_id = p.id
  INNER JOIN subscription s
    ON s.subscribable_type = 'Company' AND s.subscribable_id = d.company_id
    OR s.subscribable_type = 'User'    AND s.subscribable_id = p.analyst_id
WHERE s.user_id = 6

Some joins seem redundant:

  • discussions could be joined to subscriptions directly on the company_id column;

  • posts could be joined to subscriptions directly on the analyst_id column;

  • the last join to users in either SELECT is unnecessary as no data is retrieved from that table and the filter (users.id = 6) could be re-applied to subscriptions.user_id.

So, I would probably rewrite the query like this:

SELECT p.id
FROM posts p
  INNER JOIN discussions d ON d.post_id = p.id
  INNER JOIN subscription s
    ON s.subscribable_type = 'Company' AND s.subscribable_id = d.company_id
    OR s.subscribable_type = 'User'    AND s.subscribable_id = p.analyst_id
WHERE s.user_id = 6
娇妻 2024-12-18 12:53:31

这是未经测试的,但请尝试一下。让我知道它是否有效。

select posts.id
from posts
inner join discussions
on discussions.post_id = posts.id
inner join companies
on discussions.company_id = companies.id
inner join subscriptions
on subscriptions.subcribable_id = companies.id
inner join users
on subscriptions.user_id = users.id
or users.id = posts.analysis_id
where subscriptions.subscribable_type in ('Company', 'User')
and users.id = 6

This is untested, but give this a try. Let me know if it works.

select posts.id
from posts
inner join discussions
on discussions.post_id = posts.id
inner join companies
on discussions.company_id = companies.id
inner join subscriptions
on subscriptions.subcribable_id = companies.id
inner join users
on subscriptions.user_id = users.id
or users.id = posts.analysis_id
where subscriptions.subscribable_type in ('Company', 'User')
and users.id = 6
可是我不能没有你 2024-12-18 12:53:31

我认为我忽略了一些差异,但如果您只想将帖子放在第一个或第二个而不使用 UNION 那么:

SELECT posts.id FROM posts
where posts.id IN 
(
SELECT posts.id FROM posts
INNER JOIN discussions ON discussions.post_id = posts.id 
INNER JOIN companies ON discussions.company_id = companies.id 
INNER JOIN subscriptions ON subscriptions.subscribable_id = companies.id AND subscriptions.subscribable_type = 'Company' 
INNER JOIN users ON subscriptions.user_id = users.id WHERE users.id = 6
)
or
posts.id IN 
(
SELECT posts.id FROM posts 
INNER JOIN users users_2 ON posts.analyst_id = users_2.id 
INNER JOIN subscriptions ON subscriptions.subscribable_id = users_2.id AND subscriptions.subscribable_type = 'User'
INNER JOIN users ON subscriptions.user_id = users.id WHERE users.id = 6
)

I think there are differences I'm overlooking but if you just want the post if it's in the first or second without using a UNION then:

SELECT posts.id FROM posts
where posts.id IN 
(
SELECT posts.id FROM posts
INNER JOIN discussions ON discussions.post_id = posts.id 
INNER JOIN companies ON discussions.company_id = companies.id 
INNER JOIN subscriptions ON subscriptions.subscribable_id = companies.id AND subscriptions.subscribable_type = 'Company' 
INNER JOIN users ON subscriptions.user_id = users.id WHERE users.id = 6
)
or
posts.id IN 
(
SELECT posts.id FROM posts 
INNER JOIN users users_2 ON posts.analyst_id = users_2.id 
INNER JOIN subscriptions ON subscriptions.subscribable_id = users_2.id AND subscriptions.subscribable_type = 'User'
INNER JOIN users ON subscriptions.user_id = users.id WHERE users.id = 6
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文