如何向这个庞大的 SQL 联合语句添加联接?
我有一个声明,根据共享标签的数量和基于链接表的发言者的数量对项目进行排名,但我希望它也能够从主 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要认为在这种情况下你需要工会。如果您需要的只是计数那么为什么不尝试类似..
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
我用这段代码让它工作:
I got it working with this code: