使用内部联接时,子查询返回多个值

时间:2018-03-07 06:12:12

标签: sql subquery

我正在使用具有Inner Join的子查询,它返回10行,但我需要将这10行与另一个表匹配。 我怎么能这样做?

以下是我的询问:

SELECT 
    IT.IPTELM MOBILNO,TC.CDGACNTN GROUPACCOUNTNO, TC.CDCUSTN CUSTOMERNO, 
    TC.PRCARDN, TC.CARDNO, TC.CRDIND, TC.Limit 
FROM 
    CARDS.DBO.TOTAL_CDBS11801 TC
INNER JOIN 
    CARDS.DBO.INPARTPF IT ON (SELECT DISTINCT CDCUSTN 
                              FROM CARDS.DBO.TOTAL_CDBS11801 
                              WHERE PRCARDN IN (SELECT PRCARDN 
                                                FROM CARDS.DBO.TOTAL_CDBS11801 
                                                WHERE CARDNO IN (SELECT * 
                                                                 FROM #T)) 
                                AND CRDIND ='P') = (IT.IPKIPN)

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

你可以这样做:

select T.* from (
    SELECT 
        IT.IPTELM MOBILNO,TC.CDGACNTN GROUPACCOUNTNO, TC.CDCUSTN CUSTOMERNO, 
        TC.PRCARDN, TC.CARDNO, TC.CRDIND, TC.Limit 
    FROM 
        CARDS.DBO.TOTAL_CDBS11801 TC
    INNER JOIN 
        CARDS.DBO.INPARTPF IT ON (SELECT DISTINCT CDCUSTN 
                                  FROM CARDS.DBO.TOTAL_CDBS11801 
                                  WHERE PRCARDN IN (SELECT PRCARDN 
                                                    FROM CARDS.DBO.TOTAL_CDBS11801 
                                                    WHERE CARDNO IN (SELECT * 
                                                                     FROM #T)) 
                                    AND CRDIND ='P') = (IT.IPKIPN)
) as T
inner join Table2 on your_join_condition

答案 1 :(得分:0)

请勿尝试在ON中执行所有操作,这是WHERE子句的用途。

SELECT 
    IT.IPTELM MOBILNO,TC.CDGACNTN GROUPACCOUNTNO, TC.CDCUSTN CUSTOMERNO, 
    TC.PRCARDN, TC.CARDNO, TC.CRDIND, TC.Limit 
FROM 
    CARDS.DBO.TOTAL_CDBS11801 TC
INNER JOIN 
    CARDS.DBO.INPARTPF IT 
    ON TC.CDCUSTN = (IT.IPKIPN)

WHERE TC.CARDNO IN (SELECT * FROM #T) AND TC.CRDIND ='P'

允许联接匹配要加入的列中的所有内容,然后使用正确的where子句过滤掉不需要的结果。你得到的不仅仅是一个“中华人民共和国”。来自你的ON子句中的子查询。

SELECT DISTINCT CDCUSTN 
    FROM CARDS.DBO.TOTAL_CDBS11801 
    WHERE PRCARDN IN (SELECT PRCARDN 
        FROM CARDS.DBO.TOTAL_CDBS11801 
        WHERE CARDNO IN (SELECT * FROM #T)) 
    AND CRDIND ='P'

这不能保证给你一个结果,事实上我怀疑你想要否则只有一行会有来自第二个表的数据,但你正在以一种要求它返回的方式使用它单一值,在相等测试中。只运行该查询,如果它返回多个值,并且我下注它将返回10,因为你说#T中有大约10个值,你不能在相等测试中使用它。

如果您需要将结果加入第三个表,则会成为SubSelect

SELECT 
    ss.IPTELM MOBILNO,ss.CDGACNTN GROUPACCOUNTNO, ss.CDCUSTN CUSTOMERNO, 
    ss.PRCARDN, ss.CARDNO, ss.CRDIND, ss.Limit 
FROM 
    (SELECT * 
        FROM CARDS.DBO.TOTAL_CDBS11801 TC
        INNER JOIN CARDS.DBO.INPARTPF IT 
        ON TC.CDCUSTN = (IT.IPKIPN)
        WHERE TC.CARDNO IN (SELECT * FROM #T) AND TC.CRDIND ='P') ss
INNER JOIN yourTable yt
ON ss.X = yt.Y

答案 2 :(得分:0)

感谢您的支持。 实际上我的查询是我想要移动号码的主卡号码与二级卡号码。 情节是我有两个表 CARDS.DBO.TOTAL_CDBS11801 CARDS.DBO.INPARTPF CARDS.DBO.TOTAL_CDBS11801 有'Sec_card_number','Pri_card_numb'和'Customer_Number'等列,在此表中'Sec_card_number'和'Pri_card_numb'的Customer_number不同,因此我需要获取'Pri_card_numb' '对'Sec_card_number',然后是'Pri_card_numb'的'Customer_number',以加入另一个表 CARDS.DBO.INPARTPF ,其中包含'Customer_number'和'Mobile_number'列。 我已使用以下查询实现了所需的方案。

SELECT SCD.SECONDARY_CARD_NUMBER,IT.IPTELM
FROM dbo.BI_SecondaryCardDetails SCD 
INNER JOIN CARDS.DBO.TOTAL_CDBS11801 SC ON SCD.SECONDARY_CARD_NUMBER=SC.CARDNO
INNER JOIN CARDS.DBO.TOTAL_CDBS11801 PC ON SC.PRCARDN=PC.CARDNO
INNER JOIN CARDS.DBO.INPARTPF IT ON PC.CDCUSTN=IT.IPKIPN