Hello. I’ve been having a hard time figuring out how to do this query, and me not having a lot of experience with SQL doesn’t help at all.
I have three models/schema: Screen
, EventMedia
, and Media
.
A Screen
has many Medias
through EventMedia
.
An EventMedia
belongs to three models: Screen
, Event
, and Media
.
Media
has a type
field which can contain only two values "multimedia"
or "document"
.
A Screen
can have :medias
of both types
What I have to do is to get only Screens
that have no Media
of type
: multimedia
. Any ideas how to do this?
I’m able to do the opposite of this query (Screens
that have Media
of type
:multimedia
) using this query:
Screen
|> join(:left, [q], em in EventMedia, on: em.screen_id == q.id)
|> join(:left, [_q, em], m in Meda, on: em.media_id == m.id)
|> where([_q, em, m], not is_nil(em.id) and m.type == "multimedia")
|> distinct([q, _cm, _m], q.id)
I’ve tried this query:
Screen
|> join(:left, [q], em in EventMedia, on: em.screen_id == q.id)
|> join(:left, [_q, em], m in Meda, on: em.media_id == m.id)
|> where([_q, em, m], is_nil(em.id))
|> distinct([q, _cm, _m], q.id)
However it also filters out Screens
that only have :medias
of type: document
, which isn’t right. I’ve also tried other queries but I can’t get it right. Any ideas how to do this? Thanks!