根据当前表中2个不同列中的值有条件地加入

时间:2015-12-03 14:42:10

标签: sql join

SELECT  
    l.*,
    qqs.*,
    qq.*,
    --  cqqs.id As Cosigner_Quote_Set_ID,
    --  borrower_cosigner_quote_set_uuid as Cosigner_Quote_Set_UUID,
    --  cqq.id As Cosigner_Quote_ID,
    --  cqq.uuid As Cosigner_Quote_UUID,
    --  cqq.accept_federal_loan As Cosigner_Quote_Loan_Type,
    --  cqq.program_id As Cosigner_Quote_Program_ID,
    --  cqq.lender_name As Cosigner_Quote_Lender_Name,
    --  cqq.term_in_months As Cosigner_Loan_Term,
    --  cqq.apr As Cosigner_Loan_APR,
    --  cqq.monthly_payment As Cosigner_Loan_Pay,
    IF(tentative_quote_uuid IS NOT NULL,1,0) As Quote_Accepted,
    IF(selected_quote_uuid IS NOT NULL,1,0) As Lender_Accepted,
    loan_key As Loan_Key
FROM leads_production.leads l
LEFT JOIN greenhouse_production.citizenship_statuses csb
    ON csb.id = l.citizenship_status_ref
LEFT JOIN greenhouse_production.citizenship_statuses csc
    ON csc.id = l.cosigner_citizenship_status_ref
LEFT JOIN core_production.quotes_quote_sets qqs
    ON qqs.uuid = l.quote_set_uuid
LEFT JOIN core_production.quotes_quotes qq
    ON qq.quote_set_id = qqs.id;
-- LEFT JOIN core_production.quotes_quote_sets cqqs
--    ON cqqs.uuid = l.borrower_cosigner_quote_set_uuid
-- LEFT JOIN core_production.quotes_quotes cqq
--    ON cqq.quote_set_id = qqs.id;

请查看上述查询中的注释行。我想根据borrower_cosigner_quote_set_uuid中的值编写一个查询,其中连接是(qqs和qq表)OR(cqqs和cqq表)。

这样的事情:

If borrower_cosigner_quote_set_uuid IS NOT NULL THEN
-- LEFT JOIN core_production.quotes_quote_sets cqqs
--  ON cqqs.uuid = l.borrower_cosigner_quote_set_uuid
-- LEFT JOIN core_production.quotes_quotes cqq
--  ON cqq.quote_set_id = qqs.id;
ELSE
    LEFT JOIN core_production.quotes_quote_sets qqs
        ON qqs.uuid = l.quote_set_uuid
    LEFT JOIN core_production.quotes_quotes qq
        ON qq.quote_set_id = qqs.id;`

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您可以使用JOIN逻辑有条件OR,如下所示:

CREATE TABLE #temp1 ( foo INT );
CREATE TABLE #temp2 ( bar INT );

INSERT  INTO #temp1
        ( foo )
VALUES  ( 1 ),( 2 ),( NULL ),( 4 ),( 5 )

INSERT  INTO #temp2
        ( bar ) 
VALUES  ( 1 ),( 2 ),( 3 ),( 4 ),( 5 ),( 99 )

SELECT  *
FROM    #temp1
INNER JOIN #temp2 ON ( foo = bar AND foo IS NOT NULL)
                        OR ( foo IS NULL AND bar = 99 )


DROP TABLE #temp1
DROP TABLE #temp2

这不是一个很好的例子,但我希望JOIN NULL 99foo bar 1 1 2 2 NULL 99 4 4 5 5 来生成:

{{1}}

希望您可以按照简化的逻辑将其应用到您的问题中。

答案 1 :(得分:0)

想要在TableATableBTableC之间进行条件联接

如果TableBTableC有相似字段,您可以加入这两个表,并通过检查null值来解决使用哪个字段

<强> SqlFiddle Demo

设置测试数据库

CREATE TABLE TableA
    (`Id` varchar(4), `keyA` int, `valor` int);

INSERT INTO TableA
    (`Id`, `keyA`, `valor`)
VALUES
    ('1', 10, 90),('2', 20, 91),('3', 30, 92),(NULL, 40, 93);

CREATE TABLE TableB
    (`Id` int, `valor` int);

INSERT INTO TableB
    (`Id`, `valor`)
VALUES
    (1, 200),(2, 201),(3, 202);

CREATE TABLE TableC
    (`Id` int, `valor` int);

INSERT INTO TableC
    (`Id`, `valor`)
VALUES
    (10, 500),(20, 501),(30, 502),(40, 503);

<强> QUERY

SELECT A.*,
       IF(A.id is NULL, C.id, B.id) as joinkey,
       IF(A.id is NULL, C.valor, B.valor) as valor
FROM TableA A
LEFT JOIN TableB B
       ON A.id = B.id
LEFT JOIN TableC C
       ON A.keyA = C.id

输出

|     Id | keyA | valor | joinkey | valor |
|--------|------|-------|---------|-------|
|      1 |   10 |    90 |       1 |   200 |
|      2 |   20 |    91 |       2 |   201 |
|      3 |   30 |    92 |       3 |   202 |
| (null) |   40 |    93 |      40 |   503 |