sql - select rows with non distinct values in column 1 scoped to column 2 -


so have people table , bank_accounts table:

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 accounts owners if owner has accounts max 1 kind of currency. don't want any account owned user has account in currency. erm :p

so table want looks that:

account_id | person_id | currency  3            2(mark)     'usd' 4            2(mark)     'usd' 5            3(mary)     'eur' 

hope it's understandable. simplified example of course. use on lot bigger tables lot of data. efficiency good.

thanks lot time , help!

select *  bankaccount person_id in (   select person_id   bankaccount   group person_id   having count(distinct currency) = 1 ) 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -