mysql连接源,MySQL联接:根据源表数据选择要从哪个表联接

I find this query a bit tricky to explain; there's probably some concise wording I'm unfamilar with.

TL/DR:

How to set a SQL query to choose which table to JOIN depending on source table data?

I have a table of events. Each event relates to one of four topics; The original data was for only one topic so was a simple 1-to-1 table relationship.

But the client now wants to extend events to four different topics;

So:

Possible topics (each has their own table):

holidays | cruises | recruitment | fundays

Examples:

holidays table

id | holiday_name | other data....

------------------------------------------------

1 | basic holiday | ..etc..

2 | alpaca training | ..etc..

And

fundays table

id | funday_title | other data....

------------------------------------------------

1 | balloons! | ..etc..

2 | seaweed fun! | ..etc..

The main source of the events data is the Events Table;

Events Table

event_id | reference_id | topic_type (ENUM) | event_name | other data.....

--------------------------------------------------------------

1 | 1 | hol | something | ....

2 | 4 | cruise | some name | ....

3 | 1 | funday | horses! | ....

4 | 2 | hol | whatever | ....

So Each event has a reference table (topic_type) and a reference id (reference_id) in that table.

I am now in a position where I want to get the title of the holidays / cruises / recruitment / fundays relating to each Event. I have the event Id so the SQL would be:

SELECT event_name, etc... FROM events WHERE event_id = 1

But I also want to retrieve the name of the topic in the same query;

I have tried something like this Q&A:

SELECT events.event_name, other_table.

FROM events

CASE

LEFT JOIN holidays other_table ON events.topic_type = 'hol' AND events.reference_id = other_table.id

WHERE events.event_id = 1

And here is where I get stuck; I don't know how to dynamically reference which table to join.

I expect the output will be references to unreachable tables; if I use CASE to select tables to JOIN based on the column criteria then I envisage the SELECT will always be referencing 3 table references that are invalid so will raise issues.

I would like the output to be:

SELECT events.event_name, events.event_id, other_table.____ as topic_name ....

So that the SQL result can be:

Event_id = 1

event_id | event_name | topic_name

------------------------------------------------------------

1 | something | basic holiday

Event_id = 2

event_id | event_name | topic_name

------------------------------------------------------------

2 | some name | cruise Holiday title no.4

Event_id = 3

event_id | event_name | topic_name

------------------------------------------------------------

3 | horses! | balloons!

Is this possible?

How can this be done?

I have looked on here:

But these all seem to be either that it can't be done or that their sitations are different columns from the same table

解决方案

SELECT m.field,

COALESCE(s1.field, s2.field, s3.field, s4.field) AS field,

...

FROM main_table m

LEFT JOIN slave1_table s1 ON ...

LEFT JOIN slave2_table s2 ON ...

LEFT JOIN slave3_table s3 ON ...

LEFT JOIN slave4_table s4 ON ...

1、资项目码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资项目码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资项目码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资项目码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资项目码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资项目码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资项目码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值