I’m looking for a more efficient way to build this query (double subqueries make me cringe):
SELECT contact_id FROM (
SELECT * FROM (
SELECT mr.contact_id, di.district
FROM recipients mr
JOIN address a ON mr.contact_id = a.contact_id
JOIN district_values di ON a.id = di.entity_id
WHERE mr.mid = 29
ORDER BY di.district DESC ) addrSingle
GROUP BY mr.contact_id ) addrNull
WHERE di.district IS NULL
Let me explain what’s going on here.
Recipients holds a list of contacts. Each contact may have multiple addresses. Each address has a related district_values table. I need to retrieve contacts where the district_values.district column is null for ALL addresses.
For example:
Contact A
Address 1.district = 4
Address 2.district = null
= don't include
Contact B
Address 1.district = null
= include
Contact C
Address 1.district = null
Address 2.district = 3
= don't include
The logic of my existing query is as follows:
- retrieve contacts with related addresses and districts, order so that any addresses with a non null value are ordered first
- apply group by so i reduce to a single contact record and if addresses with a district are retained
- apply where clause to remove addresses with at least one district value
It works — it’s just a bit ugly.
You could try this, use LEFT JOIN and count the related record which is zero.