简体   繁体   中英

SQL: Joining two tables to select multiple columns referenced in another table

I got stuck at one point. I have a table structure as

在此处输入图片说明

I Need result as

在此处输入图片说明

I tried to join, Union etc of tables,but I am not able to get it. Can you please help me.Thanks in advance.

Assuming your first table is Ring and the second one is RingTone

You have to JOIN to the Ring table twice:

SELECT
    rt.ID,
    DayRingTone = r1.RingName,
    NightRingTone = r2.RingName
FROM RingTone rt
INNER JOIN Ring r1
    ON rt.DayRingTone = r1.RingID
INNER JOIN Ring r2
    ON rt.NightRingTone = r2.RingID

Try this way

SELECT
    rt.ID,
    DayRingTone = b.RingName,
    NightRingTone = c.RingName
FROM tableringtone a
INNER JOIN tablering b
    ON a.DayRingTone = b.RingID
INNER JOIN tablering c
    ON a.NightRingTone = c.RingID

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