简体   繁体   English

SQL:连接两个表以选择另一个表中引用的多个列

[英]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 假设您的第一个表是Ring ,第二个表是RingTone

You have to JOIN to the Ring table twice: 您必须两次JOIN Ring表:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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