我需要一个SQL查询,如下所示

时间:2016-09-29 19:07:38

标签: sql

这是我的以下问题,请查看代码段。

嗨,我有两张表A,表B如下:

<ul class="flagMenu">
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
</ul>

<br><br>

<ul class="flagMenu">
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
    <li><a href=""><img src="" width="16" height="11" alt=""></a></li>
</ul>

我需要如下结果:

**Table A**                 **Table B**
Reg Id (ex: 217)              Session Name (ex: Section A, Section B)
                              First Choice
                              Second Choice
                              Third Choice
                              Reg Id

1 个答案:

答案 0 :(得分:0)

以下是我如何做到这一点。加入每一栏。

SELECT A.RegID,
       BSecA.FirstChoice || ', ' || BSecA.SecondChoice || ', ' || BSecA.ThirdChoice as SectionA,
       BSecB.FirstChoice || ', ' || BSecB.SecondChoice || ', ' || BSecB.ThirdChoice as SectionB,
       BSecC.FirstChoice || ', ' || BSecC.SecondChoice || ', ' || BSecC.ThirdChoice as SectionC 
FROM TableA AS A
LEFT JOIN TableB AS BsecA ON A.RegID = BsecA.RegID AND BsecA.SectionName = "Section A"
LEFT JOIN TableB AS BsecB ON A.RegID = BsecB.RegID AND BsecB.SectionName = "Section B"
LEFT JOIN TableB AS BsecC ON A.RegID = BsecC.RegID AND BsecC.SectionName = "Section C"