基于子查询的两个条件。如何缩短它?
Two conditions based on subquery. How to shorten it?
我有基于 2 个子查询的工作查询。
SELECT * FROM database1
where account_id in (SELECT account_id FROM (select account_id,transaction_id from ... here i have my big sql query))
and transaction_id in (SELECT transaction_id FROM (select account_id,transaction_id from ... here i have my big sql query))
我想知道如何做才能不必粘贴 2 次我的大 sql 查询而只在一行中使用这两个条件?这可能吗?
我猜你真的想要:
select d.*
from database1 d
where exists (select 1
from <big query here> q
where q.account_id = d.account_id and
q.transaction_id = d.transaction_id
);
与您的查询不同,这保证了两列在您的查询中位于同一行。这不是您查询的目的,但我猜这是您的意图。
如果你真的想检查其中任何一个是否发生在任何地方,那么你可以使用 CTE:
with q as (
<your query here>
)
select d.*
from database1 d
where exists (select 1 from q where q.account_id = d.account_id) and
exists (select 1 from q where q.transaction_id = d.transaction_id);
我有基于 2 个子查询的工作查询。
SELECT * FROM database1
where account_id in (SELECT account_id FROM (select account_id,transaction_id from ... here i have my big sql query))
and transaction_id in (SELECT transaction_id FROM (select account_id,transaction_id from ... here i have my big sql query))
我想知道如何做才能不必粘贴 2 次我的大 sql 查询而只在一行中使用这两个条件?这可能吗?
我猜你真的想要:
select d.*
from database1 d
where exists (select 1
from <big query here> q
where q.account_id = d.account_id and
q.transaction_id = d.transaction_id
);
与您的查询不同,这保证了两列在您的查询中位于同一行。这不是您查询的目的,但我猜这是您的意图。
如果你真的想检查其中任何一个是否发生在任何地方,那么你可以使用 CTE:
with q as (
<your query here>
)
select d.*
from database1 d
where exists (select 1 from q where q.account_id = d.account_id) and
exists (select 1 from q where q.transaction_id = d.transaction_id);