[英]Conditional JOIN using field data
I have the following tables: 我有以下表格:
dog : id, name, event_id
which is used to store dogs dog : id, name, event_id
用于存储狗的id, name, event_id
event : id, title, owner_id
which is used to store events like birthday, vaccines, etc event : id, title, owner_id
,用于存储生日,疫苗等事件
owner : id, name
which is used to store owners. owner : id, name
用于存储所有者的名称。 The original owner is a kennel which has an id of 1
原始所有者是ID为1
的狗窝
owner_event : original_owner_event_id, other_owner_event_id
which is used to equate various owner events to each other, ie the original owner might consider a "walk event" equal to the other owner's "play catch" event. owner_event : original_owner_event_id, other_owner_event_id
,用于将各种所有者事件彼此等同,即原始所有者可能认为“步行事件”与另一个所有者的“比赛接球”事件相同。
Joining everything: 加入一切:
dog joins event where dog.event_id = event.id
狗加入事件 ,其中dog.event_id = event.id
owner joins onto event where event.owner_id = owner.id
所有者加入事件 ,其中event.owner_id = owner.id
Here's the tricky part : 这是棘手的部分 :
When owner_id > 1
then event joins owner_event where owner_event.other_owner_event_id = event.id
当owner_id > 1
事件将加入owner_event ,其中owner_event.other_owner_event_id = event.id
Otherwise, event joins owner_event where owner_event.original_owner_event_id = event.id
. 否则, 事件将加入owner_event ,其中owner_event.original_owner_event_id = event.id
。
So far I have the following: 到目前为止,我有以下内容:
SELECT * FROM dog
LEFT JOIN event `event` ON `event`.id = dog.event_id
LEFT JOIN owner `owner` ON `owner`.id = `event`.owner_id
LEFT JOIN owner `owner_event_other` ON `owner_event_other`.other_owner_event_id = `event`.id AND `owner`.id > 1
LEFT JOIN owner `owner_event_original` ON `owner_event_original`.original_owner_event_id = `event`.id AND `owner`.id = 1
WHERE dog.id = 3
I'm just not sure how to actually get the event.title
now for dog 3
(for example). 我只是不确定如何立即获得狗3
的event.title
(例如)。
Bearing in mind that it could be the event.id
from owner_event.other_owner_event_id
column or owner_event.original_owner_event_id
column depending on the owner.id
. 铭记,这可能是event.id
从owner_event.other_owner_event_id
列或owner_event.original_owner_event_id
列取决于owner.id
。
As only one of the two possibilities can exist for a given owner
record, I would suggest to only join owner_event
once, but chain event
a second time to it in order to retrieve the title via the original owner's event (in case the directly retrieved event
record does not represent that title. With COALESCE
you can then pick the right title. 由于只有两个可能性,对于给定存在一个owner
的记录,我建议只加入owner_event
一次,但链event
第二次将它,以便检索经原主人的事件的标题(如果直接检索到的event
记录不代表该标题。使用COALESCE
您可以选择正确的标题。
SELECT dog.*,
COALESCE(original_event.title, event.title) AS original_event_title
FROM dog
LEFT JOIN event
ON event.id = dog.event_id
LEFT JOIN owner
ON owner.id = event.owner_id
LEFT JOIN owner_event
ON owner_event.other_owner_event_id = event.id
LEFT JOIN event original_event
ON original_event.id = owner_event.original_owner_event_id
WHERE dog.id = 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.