Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7309113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:40:20+00:00 2026-05-28T23:40:20+00:00

I’m looking for a more efficient way to build this query (double subqueries make

  • 0

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:

  1. retrieve contacts with related addresses and districts, order so that any addresses with a non null value are ordered first
  2. apply group by so i reduce to a single contact record and if addresses with a district are retained
  3. apply where clause to remove addresses with at least one district value

It works — it’s just a bit ugly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:40:21+00:00Added an answer on May 28, 2026 at 11:40 pm

    You could try this, use LEFT JOIN and count the related record which is zero.

    SELECT mr.contact_id 
        FROM recipients mr 
    LEFT JOIN address a ON mr.contact_id = a.contact_id  
    LEFT JOIN district_values di ON a.id = di.entity_id 
        WHERE mr.mid = 29 
    GROUP BY mr.contact_id
        HAVING COUNT(a.*) = 0 AND COUNT(di.*) = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am currently running into a problem where an element is coming back from
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.