[英]select rows with non distinct values in column 1 scoped to column 2
So i have a people table and bank_accounts table: 所以我有一个人表和bank_accounts表:
People
id | name
1 John
2 Mark
3 Mary
BankAccount
id | person_id | currency
1 1 'USD'
2 1 'EUR'
3 2 'USD'
4 2 'USD'
5 3 'EUR'
I want to get back all accounts with their owners if that owner has only accounts with max one kind of currency. 如果该所有者只有最多一种货币的账户,我想找回所有与其所有者的账户。 I don't want to get back any account which is owned by user which has another account in another currency. 我不想找回由用户拥有的任何帐户,该用户的另一个帐户使用另一种货币。 erm :P erm:P
so the table i want to get back looks like that: 所以我想找回来的桌子是这样的:
account_id | person_id | currency
3 2(Mark) 'USD'
4 2(Mark) 'USD'
5 3(Mary) 'EUR'
Hope it's understandable. 希望这是可以理解的。 This is simplified example of course. 当然,这是简化的示例。 I will use this on a lot bigger tables with a lot of data. 我将在具有大量数据的更大表上使用此功能。 So some efficiency would be also good. 因此,一些效率也将很好。
Thanks a lot for your time and help! 非常感谢您的时间和帮助!
select *
from bankAccount
where person_id in
(
select person_id
from bankAccount
group by person_id
having count(distinct currency) = 1
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.