Dany
Dany

Reputation: 2174

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

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

enter image description here

I Need result as

enter image description here

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

Upvotes: 1

Views: 48

Answers (2)

Felix Pamittan
Felix Pamittan

Reputation: 31879

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

Upvotes: 4

Mukesh Kalgude
Mukesh Kalgude

Reputation: 4844

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

Upvotes: 2

Related Questions