diff --git a/queries.sql b/queries.sql index 4cd18d5c..731e4cf9 100644 --- a/queries.sql +++ b/queries.sql @@ -323,13 +323,13 @@ SELECT subscribers.id, subscribers.created_at, subscribers.updated_at FROM subscribers - LEFT JOIN subscriber_lists sl + LEFT JOIN subscriber_lists ON ( -- Optional list filtering. (CASE WHEN CARDINALITY($1::INT[]) > 0 THEN true ELSE false END) - AND sl.subscriber_id = subscribers.id + AND subscriber_lists.subscriber_id = subscribers.id ) - WHERE sl.list_id = ALL($1::INT[]) AND id > $2 + WHERE subscriber_lists.list_id = ALL($1::INT[]) AND id > $2 AND (CASE WHEN CARDINALITY($3::INT[]) > 0 THEN id=ANY($3) ELSE true END) %query% ORDER BY subscribers.id ASC LIMIT (CASE WHEN $4 < 1 THEN NULL ELSE $4 END);