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 6541023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:03:11+00:00 2026-05-25T11:03:11+00:00

I have the logic worked out, just not sure how to best write this

  • 0

I have the logic worked out, just not sure how to best write this query.

the logic is

we have a deal ID of 1
a deal is linked to multiple regions
a deal is linked to multiple interests
a user is linked to multiple regions
a user is linked to multiple interests
we want all users where….

the user is linked to the same region as a deal

userRegionLink url, dealRegionLink drl
url.regionId is in drl.regionId where drl.dealId = 1
the user is linked to the same interest as a deal
userInterestLink uil, dealInterestLink dil
uil.interestId is in dil.interestId where dil.dealId = 1
this would give us a list of the users
now we need to select distinct from the list so we only end up sending each user a single email

But I have no idea what the best way to write this query would be.

We are dealing with a few tables here

We have

users which has all the user Information in it userId and other columns not important

userInterestLink which has userId and interestId
dealInterestLink which has dealId and interestId

userRegionLink which has userId and regionId
dealRegionLink which has dealId and regionId

so what we are wanting in the end is all the user info which matches.

  • 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-25T11:03:11+00:00Added an answer on May 25, 2026 at 11:03 am

    I take RC’s answer and modify it

    SELECT u.userId, uil.interestId, url.regionId FROM users u 
      JOIN userInterestLink uil ON (uil.userId = u.userId) 
      JOIN userRegionLink url ON (url.userId = u.userId) 
    WHERE interestId IN (
      SELECT DISTINCT interestId FROM dealInterestLink WHERE dealId = 1
    ) AND regionId IN (
      SELECT DISTINCT regionId FROM dealRegionLink WHERE dealId = 1
    )
    

    as there is no need for LEFT JOIN if I exclude the NULL rows afterwards.

    A more “symmetric” version without subqueries and with USING would be

    SELECT u.userId, uil.interestId, url.regionId FROM users u 
      JOIN userInterestLink uil USING (userId)
      JOIN userRegionLink url USING (userId)
      JOIN dealInterestLink dil USING (interestId)
      JOIN dealRegionLink drl USING (regionId, dealId)
    WHERE dealId = 1
    

    Untested as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have worked on single threaded business logic/back-end programming for most of my career.
I'm sure you've all seen them. Line of Business UIs that have logic such
I have magento installed in a subdirectory. www.domain.com/subdir/magento This site worked perfectly at one
I have a PHP website I'm maintaining and I've confirmed that this worked at
I have some logic, which defines and uses some user-defined types, like these: class
I have authentication logic that uses cookies. The model attributes are initialized from cookies,
I have some logic in a method that operates on a specified type and
I have some logic that depends upon two properties being set, as it executes
I have some logic in before_save whereby (only) when some conditions are met I
I recently inherited a VBA macro that needs to have validation logic added to

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.