1

有人可以帮我吗,我有 2 张桌子

财务账户货币映射和自有账户。

我想选择字段 legalEntity_id 的金融账户货币映射。

这是我到目前为止所拥有的。

SELECT 
FROM financeaccountcurrencymapping 
WHERE ownedAccount_id EXISTS IN (
                SELECT id 
                FROM ownedaccount 
                WHERE legalEntity_id = ??);

表中的字段是:

'财务账户货币映射'

  • ID
  • 货币货币
  • accountForCash_id
  • 拥有的Account_id

'自有帐户'

  • ID
  • 姓名
  • legalEntity_id
  • 资产或负债
  • 资产类型
  • 存款类型
  • 责任类型
  • 多种货币
  • 货币
  • 描述
4

4 回答 4

4

在这种情况下,您不会使用 EXISTS。单独的 IN 就可以了。

SELECT 
FROM financeaccountcurrencymapping 
WHERE ownedAccount_id IN (SELECT id FROM ownedaccount WHERE legalEntity_id = ??);
于 2012-11-26T17:11:54.033 回答
1

You're original query is really close. You just need to remove the EXISTS, like so:

SELECT *
FROM financeaccountcurrencymapping 
WHERE ownedAccount_id IN (
                SELECT id 
                FROM ownedaccount 
                WHERE legalEntity_id = ??);

Alternatively, you could write this query with a JOIN for the same effective result.

SELECT f.*
FROM financeaccountcurrencymapping AS f 
INNER JOIN ownedaccount AS o ON (f.id = o.ownedAccount_id) 
WHERE o.legalEntity_id = ??;
于 2012-11-26T17:14:04.463 回答
1

如果您真的想使用 EXISTS 或想查看如何使用它的示例,我认为以下内容会起作用。就个人而言,我可能会使用 IN 代替。

SELECT 
FROM financeaccountcurrencymapping as f
WHERE EXISTS (SELECT id 
              FROM ownedaccount 
              WHERE f.ownedAccount_id = id AND legalEntity_id = ??);
于 2012-11-26T17:24:36.613 回答
1
SELECT legalEntity_id
FROM financeaccountcurrencymapping 
WHERE ownedAccount_id IN (
                SELECT id 
                FROM ownedaccount 
                WHERE legalEntity_id = ??);
于 2012-11-26T17:13:11.657 回答