如何向这个庞大的 SQL 联合语句添加联接?

发布于 2024-10-19 15:27:50 字数 850 浏览 6 评论 0原文

我有一个声明,根据共享标签的数量和基于链接表的发言者的数量对项目进行排名,但我希望它也能够从主 tbl_talks 中提取 talk_title 和 talk_brief 信息,而该信息现在实际上不在查询中(仅使用链接表)。 基本上链接表有talk_id和tag_id或talk_id和speaker_id。

我尝试了几种尝试 LEFT JOIN 的方法,或者在应用联合的配对 SELECT 中添加 tbl_talks 但没有运气。我需要将 tbl_linking_talk_tag.talk_id 和 tbl_linking_speakers_talk 与 tbl_talks.talk_id 关联起来,然后从 tbl_talks 中获取 talk_title 和 talk_brief

SELECT talk_id, count(*) AS common_term_count FROM 
      (
      SELECT talk_id FROM tbl_linking_talk_tag WHERE tag_id IN 
         (SELECT tag_id FROM tbl_linking_talk_tag WHERE talk_id = 1)
      UNION ALL 
      SELECT talk_id FROM tbl_linking_speakers_talk WHERE speaker_id IN
         (SELECT speaker_id FROM tbl_linking_speakers_talk WHERE talk_id = 1)
       ) AS accumulator_table

 WHERE talk_id != 1 

 GROUP BY talk_id

 ORDER BY common_term_count DESC

I have a statement that ranks items based on number of shared tags and speakers based on linking tables, but I want it to also be able to pull the talk_title and talk_brief info from the main tbl_talks which isn't actually in the query right now (just the linking tables are used).
basically the linking tables have talk_id and tag_id or talk_id and speaker_id.

I tried a few ways of trying to LEFT JOIN, or adding tbl_talks inside the paired SELECTs that the union is applied to but had no luck. I need to associate both the tbl_linking_talk_tag.talk_id and tbl_linking_speakers_talk with tbl_talks.talk_id and with that, grab talk_title and talk_brief from tbl_talks

SELECT talk_id, count(*) AS common_term_count FROM 
      (
      SELECT talk_id FROM tbl_linking_talk_tag WHERE tag_id IN 
         (SELECT tag_id FROM tbl_linking_talk_tag WHERE talk_id = 1)
      UNION ALL 
      SELECT talk_id FROM tbl_linking_speakers_talk WHERE speaker_id IN
         (SELECT speaker_id FROM tbl_linking_speakers_talk WHERE talk_id = 1)
       ) AS accumulator_table

 WHERE talk_id != 1 

 GROUP BY talk_id

 ORDER BY common_term_count DESC

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

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

发布评论

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

评论(2

相权↑美人 2024-10-26 15:27:50

不要认为在这种情况下你需要工会。如果您需要的只是计数那么为什么不尝试类似..

SELECT talk_id,
(SELECT count(*) FROM tbl_linking_talk_tag sub WHERE tag_id IN
(从 tbl_linking_talk_tag 中选择 tag_id,其中 talk_id = 1) 和 maintalk.talk_id=sub.talk_id) +
(SELECT count(*) FROM tbl_linking_speakers_talk sub1 WHERE slider_id IN
(从tbl_linking_speakers_talk中选择speaker_id,其中talk_id = 1)和maintalk.talk_id = sub1.talk_id
),talk_title,talk_brief
来自主讲
其中 talk_id != 1
group by talk_id

语法可能需要一些修复,但方法是

从 maintable 中选择列

Don't think you need a union in this case. If all you need is count then why not try something like..

SELECT talk_id,
(SELECT count(*) FROM tbl_linking_talk_tag sub WHERE tag_id IN
(SELECT tag_id FROM tbl_linking_talk_tag WHERE talk_id = 1) and maintalk.talk_id=sub.talk_id) +
(SELECT count(*) FROM tbl_linking_speakers_talk sub1 WHERE speaker_id IN
(SELECT speaker_id FROM tbl_linking_speakers_talk WHERE talk_id = 1) and and maintalk.talk_id=sub1.talk_id
),talk_title,talk_brief
from maintalk
where talk_id != 1
group by talk_id

The syntax may need some fixing but the approach is

select columns, from maintable

红尘作伴 2024-10-26 15:27:50

我用这段代码让它工作:

 SELECT a.talk_id, a.talk_image, a.talk_title, a.talk_brief, count(b.talk_id)  AS common_term_count 
        FROM tbl_talks a LEFT OUTER JOIN
             (
             SELECT talk_id FROM tbl_linking_talk_tag WHERE tag_id IN 
                (SELECT tag_id FROM tbl_linking_talk_tag WHERE talk_id = 1)
             UNION ALL 
             SELECT talk_id FROM tbl_linking_speakers_talk WHERE speaker_id IN
                (SELECT speaker_id FROM tbl_linking_speakers_talk WHERE talk_id = 1) 
             ) AS b
             ON a.talk_id = b.talk_id       
        WHERE a.talk_id != 1        
        GROUP BY b.talk_id      
        ORDER BY common_term_count DESC

I got it working with this code:

 SELECT a.talk_id, a.talk_image, a.talk_title, a.talk_brief, count(b.talk_id)  AS common_term_count 
        FROM tbl_talks a LEFT OUTER JOIN
             (
             SELECT talk_id FROM tbl_linking_talk_tag WHERE tag_id IN 
                (SELECT tag_id FROM tbl_linking_talk_tag WHERE talk_id = 1)
             UNION ALL 
             SELECT talk_id FROM tbl_linking_speakers_talk WHERE speaker_id IN
                (SELECT speaker_id FROM tbl_linking_speakers_talk WHERE talk_id = 1) 
             ) AS b
             ON a.talk_id = b.talk_id       
        WHERE a.talk_id != 1        
        GROUP BY b.talk_id      
        ORDER BY common_term_count DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文