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
)
ORDER BY t.date

但是,我想对此进行修改,例如,如果联结表virtual_account-account不包含任何联结 where ,则应忽略vaa.virtual_account = 3相应的条件。t.id IN (...)到目前为止,我还没有找到解决方案,所以任何帮助都将不胜感激。

4

4 回答 4

0

只是两个条件之间的OR :

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

如果一个没有返回任何东西,它仍然会返回其他事务。

于 2013-07-27T11:57:25.443 回答
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)。否则,您可能会遇到多个匹配导致多行的问题。

于 2013-07-27T12:00:22.230 回答
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
于 2013-07-27T12:05:09.703 回答
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
于 2013-07-27T12:06:58.070 回答