2013-07-27 87 views
0

我從表中使用以下查詢選擇交易:如果子查詢沒有找到聯結,如何忽略WHERE IN子句?

SELECT t.* 
FROM transactions AS t 
WHERE t.id IN 
(
    SELECT t2.id 
    FROM `virtual_account-account` AS vaa 
    LEFT JOIN transactions AS t2 
    ON t2.account = vaa.account 
    WHERE vaa.virtual_account = 3 
) 
AND t.id IN 
(
    SELECT tt.transaction 
    FROM `virtual_account-tag` AS vat 
    LEFT JOIN `transaction-tag` AS tt 
    ON tt.tag = vat.tag 
    WHERE vat.virtual_account = 3 
) 
ORDER BY t.date 

不過,我想修改該這樣,如果,例如,結合表virtual_account-account不包含任何路口,其中vaa.virtual_account = 3,然後應該忽略相應的t.id IN (...)條件。到目前爲止,我沒有找到解決方案,所以任何幫助將不勝感激。

+0

怎麼樣,如果兩個子選擇返回任何結果?你想顯示所有交易還是僅顯示所有交易? –

+0

@FabianBigler其實,我不太在意這種情況,因爲這是不可能發生的。然而,空的結果會比選擇所有交易更好。 – Niko

回答

0

只是一個兩個條件之間:

SELECT t.* 
FROM transactions AS t 
WHERE t.id IN 
(
    SELECT t2.id 
    FROM `virtual_account-account` AS vaa 
    LEFT JOIN transactions AS t2 
    ON t2.account = vaa.account 
    WHERE vaa.virtual_account = 3 
) 
OR t.id IN 
(
    SELECT tt.transaction 
    FROM `virtual_account-tag` AS vat 
    LEFT JOIN `transaction-tag` AS tt 
    ON tt.tag = vat.tag 
    WHERE vat.virtual_account = 3 
) 
ORDER BY t.date 

如果一個人沒有返回,它仍然會返回其他交易。

+0

他說它應該被忽略 –

+0

感謝您的迴應,但不幸的是,並非如此簡單:如果在ID爲3的兩個聯結表(虛擬賬戶 - 賬戶或虛擬賬戶 - 標籤)都有條目,那麼應該有AND條件。 – Niko

0

以下方法將子查詢轉換爲聯接並使用變量來計算子查詢中的行數。該where條款然後使用這個變量爲你想要的邏輯:

SELECT t.* 
FROM transactions t left outer join 
    (SELECT t2.id, @cnt := @cnt + 1 
     FROM `virtual_account-account` AS vaa 
      LEFT JOIN transactions AS t2 
      ON t2.account = vaa.account cross join 
      (select @cnt := 0) 
     WHERE vaa.virtual_account = 3 
     group by t2.id 
    ) c1 
    on c1.id = t.id 
WHERE (c1.id is not null or @cnt = 0) and 
     t.id IN (SELECT tt.transaction 
       FROM `virtual_account-tag` AS vat 
        LEFT JOIN `transaction-tag` AS tt 
        ON tt.tag = vat.tag 
       WHERE vat.virtual_account = 3 
    ) 
ORDER BY t.date; 

注意,子查詢消除重複(使用group by)。否則,您可能會遇到導致多行的多個匹配問題。

0

有點笨拙和冗長,但是......

SELECT t.* 
FROM transactions AS t 
WHERE t.id IN 
(
    SELECT t2.id 
    FROM `virtual_account-account` AS vaa 
    LEFT JOIN transactions AS t2 
    ON t2.account = vaa.account 
    WHERE vaa.virtual_account = 3 
) 
AND t.id IN 
(
    SELECT tt.transaction 
    FROM `virtual_account-tag` AS vat 
    LEFT JOIN `transaction-tag` AS tt 
    ON tt.tag = vat.tag 
    WHERE vat.virtual_account = 3 
    AND EXISTS 
    (
     SELECT t2.id 
     FROM `virtual_account-account` AS vaa 
     LEFT JOIN transactions AS t2 
     ON t2.account = vaa.account 
     WHERE vaa.virtual_account = 3 
    ) 
) 
ORDER BY t.date 
0

嘗試:

SELECT t.* 
FROM (select count(*) vaa 
     FROM `virtual_account-account` 
     WHERE virtual_account = 3) a 
CROSS JOIN (select count(*) vat 
      FROM `virtual_account-tag` 
      WHERE virtual_account = 3) g 
CROSS JOIN transactions AS t 
LEFT JOIN `virtual_account-account` vaa 
     ON t.id = vaa.id AND vaa.virtual_account = 3 
LEFT JOIN `virtual_account-tag` vat 
    JOIN `transaction-tag` tt ON tt.tag = vat.tag 
     ON t.id = tt.transaction AND vat.virtual_account = 3 
WHERE (a.vaa = 0 OR vaa.id IS NOT NULL) AND 
     (g.vat = 0 OR vat.tag IS NOT NULL) 
GROUP BY t.id 
ORDER BY t.date 
相關問題