gpt4 book ai didi

mysql - 比较两个 SQL 查询的输出?

转载 作者:行者123 更新时间:2023-11-29 18:17:07 25 4
gpt4 key购买 nike

我在执行复杂的 SQL 查询时遇到了一些问题,如果有任何帮助,我将不胜感激。我简化了表格和名称。

SCHEMA:   

Business Account
payment_account_id
business_id

Business Misc
business_user_id
business_id

Payment Account
business_user_id
payment_account_id

我需要一个 SQL 查询来获取拥有多个付款帐户的所有企业、拥有多个付款帐户的所有企业用户,并在这些付款帐户具有相同 ID 时提供重叠信息。

到目前为止我已经得到:

business_with_multiple_accounts = 

select *
from "Business Account"
group by business_id
having count(distinct payment_account_id) > 1;

users_with_multiple_accounts =

select *
from "Payment Account"
group by business_user_id
having count(distinct payment_account_id) > 1;

我想我只需要比较这两个输出(但我可能是错的!),但我不确定如何继续。我尝试过以各种方式嵌套查询,以及在三向联接中使用“Business Misc”表。

感谢您提供的任何帮助!

最佳答案

请尝试这个:

business_with_multiple_accounts = 
select *
from business_acct
where
(Select Distinct count(*)
from
payment_acct
where payment_acct.payment_acct_id= business_acct.payment_acct_id
)>1
group by business_id

users_with_multiple_accounts =
select *
from business_misc
where
(Select Distinct count(*)
from
payment_acct
where payment_acct.business_user_id= business_misc.business_user_id
)>1
group by business_user_id

关于mysql - 比较两个 SQL 查询的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46923854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com