Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join table and pick rows where for given id exists only one value

I don't know, if I made good title, but please let me visualize this.

enter image description here

So I have two tables and for given case I need to select row where payment currency was ONLY in EUR.

Correct document Id's will be: 2, 3, 4, 5

These are overall bigger tables with 900k+ records.

Can you please suggest me how query should look?

like image 248
Grzegorz G. Avatar asked Dec 01 '25 04:12

Grzegorz G.


1 Answers

use correlated subquery with not exists

select distinct a.document_id from tablename a inner join tablename b b on a.document_id=b.payment_docid
where not exists 
   (select 1 from tablename b1 where b1.payment_docid=b.payment_docid and currency<>'EUR')
like image 71
Fahmi Avatar answered Dec 02 '25 18:12

Fahmi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!