简体   繁体   中英

Two joins between two tables on the same field

I have a table in a MySQL database V_Activity_SameTimeActivity with two fields SameTimeActivityId and ActivityId. They are both fields which reference the Id field from ACTIVITY_V table. I want to create a view that shows me the ACTIVITY_V.Name field for both sets of Id in the V_Activity_SameTimeActivity table. I can get one but not the other. This the query so far.

SELECT
ACTIVITY_V.Name AS MasterActivity,
V_Activity_SameTimeActivity.ActivityId,
V_Activity_SameTimeActivity.SameTimeActivityId

FROM V_Activity_SameTimeActivity
INNER JOIN ACTIVITY_V ON V_Activity_SameTimeActivity.ActivityId = ACTIVITY_V.Id;

I also want ACTIVITY_V.Name AS SametimeActivity to show with V_Activity_SameTimeActivity.SameTimeActivityId joined on ACTIVITY_V.Id

you can join again ACTIVITY_V for your SameTimeActivityId mapping

SELECT
    t2.Name AS MasterActivity,
    t3.Name AS SameActivityName,
    t1.ActivityId,
    t1.SameTimeActivityId
FROM V_Activity_SameTimeActivity t1
LEFT JOIN ACTIVITY_V t2 ON t1.ActivityId = t2.Id;
LEFT JOIN ACTIVITY_V t3 ON t3.ActivityId = t1.SameTimeActivityId;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM