M
M
Maksim2020-09-29 16:20:42
PostgreSQL
Maksim, 2020-09-29 16:20:42

How to write a request of this type?

Hello, I can not write a request. Maybe someone will tell.

There is table A (one) and table B (many)
connected through A.id and B.a_id, in B there is a field (string)

I have an array of strings for example: ['x','y'] (assumed values ​​B .field) from which I want to get:

all records of table A that has records B.field = 'x' and B.field = 'y' in the relationship (+ there may be others, the main thing is that these 2 values ​​are exactly in the relationship)

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vilinyh, 2020-09-29
@goncharoff-ms

select * from A
where id (
   select a_id from B
   where field in ('x', 'y')
   group by a_id
   having count(*) = 2
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question